Skip to content

Commit 44cf919

Browse files
bobwhitelockhemanth
authored andcommitted
Use consistent variable declarations (#84)
Not a big deal, but generally when writing ES2015+ it's preferable to use `const` where possible for variable declarations, as it provides a guarantee to the reader that you won't change what the variable references, and preference `let` over `var` otherwise , as this at least guarantees that the variable will be block-scoped rather than function-scoped. Being inconsistent with this means readers must sometimes have to consider whether a difference is there for a reason or not.
1 parent 2b2d079 commit 44cf919

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

readme.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,14 @@ Partially applying a function means creating a new function by pre-filling some
9292
```js
9393
// Helper to create partially applied functions
9494
// Takes a function and some arguments
95-
let partial = (f, ...args) =>
95+
const partial = (f, ...args) =>
9696
// returns a function that takes the rest of the arguments
9797
(...moreArgs) =>
9898
// and calls the original function with all of them
9999
f(...[...args, ...moreArgs]);
100100

101101
// Something to apply
102-
let add3 = (a, b, c) => a + b + c;
102+
const add3 = (a, b, c) => a + b + c;
103103

104104
// Partially applying `2` and `3` to `add3` gives you a one-argument function
105105
const fivePlus = partial(add3, 2, 3); // (c) => 2 + 3 + c
@@ -150,7 +150,7 @@ A function is pure if the return value is only determined by its
150150
input values, and does not produce side effects.
151151

152152
```js
153-
let greet = (name) => "Hi, " + name ;
153+
const greet = (name) => "Hi, " + name ;
154154

155155
greet("Brianne") // "Hi, Brianne"
156156

@@ -160,9 +160,9 @@ As opposed to:
160160

161161
```js
162162

163-
let greeting;
163+
const greeting;
164164

165-
let greet = () => greeting = "Hi, " + window.name;
165+
const greet = () => greeting = "Hi, " + window.name;
166166

167167
greet(); // "Hi, Brianne"
168168

@@ -173,7 +173,7 @@ greet(); // "Hi, Brianne"
173173
A function or expression is said to have a side effect if apart from returning a value, it interacts with (reads from or writes to) external mutable state.
174174

175175
```js
176-
var differentEveryTime = new Date();
176+
const differentEveryTime = new Date();
177177
```
178178

179179
```js
@@ -202,16 +202,16 @@ Writing functions where the definition does not explicitly identify the argument
202202

203203
```js
204204
// Given
205-
let map = (fn) => (list) => list.map(fn);
206-
let add = (a) => (b) => a + b;
205+
const map = (fn) => (list) => list.map(fn);
206+
const add = (a) => (b) => a + b;
207207

208208
// Then
209209

210210
// Not points-free - `numbers` is an explicit argument
211-
let incrementAll = (numbers) => map(add(1))(numbers);
211+
const incrementAll = (numbers) => map(add(1))(numbers);
212212

213213
// Points-free - The list is an implicit argument
214-
let incrementAll2 = map(add(1));
214+
const incrementAll2 = map(add(1));
215215
```
216216

217217
`incrementAll` identifies and uses the parameter `numbers`, so it is not points-free. `incrementAll2` is written just by combining functions and values, making no mention of its arguments. It __is__ points-free.
@@ -346,7 +346,7 @@ behavior of the program is said to be referentially transparent.
346346
Say we have function greet:
347347

348348
```js
349-
let greet = () => "Hello World!";
349+
const greet = () => "Hello World!";
350350
```
351351

352352
Any invocation of `greet()` can be replaced with `Hello World!` hence greet is
@@ -361,15 +361,15 @@ When an application is composed of expressions and devoid of side effects, truth
361361
Lazy evaluation is a call-by-need evaluation mechanism that delays the evaluation of an expression until its value is needed. In functional languages, this allows for structures like infinite lists, which would not normally be available in an imperative language where the sequencing of commands is significant.
362362

363363
```js
364-
let rand = function*() {
364+
const rand = function*() {
365365
while (1 < 2) {
366366
yield Math.random();
367367
}
368368
}
369369
```
370370

371371
```js
372-
let randIter = rand();
372+
const randIter = rand();
373373
randIter.next(); // Each execution gives a random value, expression is evaluated on need.
374374
```
375375

@@ -412,8 +412,8 @@ The identity value is empty array `[]`
412412
If identity and compose functions are provided, functions themselves form a monoid:
413413

414414
```js
415-
var identity = (a) => a;
416-
var compose = (f, g) => (x) => f(g(x));
415+
const identity = (a) => a;
416+
const compose = (f, g) => (x) => f(g(x));
417417

418418
compose(foo, identity) ≍ compose(identity, foo) ≍ foo
419419
```
@@ -513,11 +513,11 @@ Make array a setoid:
513513

514514
```js
515515
Array.prototype.equals = (arr) => {
516-
var len = this.length
516+
const len = this.length
517517
if (len !== arr.length) {
518518
return false
519519
}
520-
for (var i = 0; i < len; i++) {
520+
for (let i = 0; i < len; i++) {
521521
if (this[i] !== arr[i]) {
522522
return false
523523
}
@@ -542,7 +542,7 @@ An object that has a `concat` function that combines it with another object of t
542542
An object that has a `reduce` function that can transform that object into some other type.
543543

544544
```js
545-
let sum = (list) => list.reduce((acc, val) => acc + val, 0);
545+
const sum = (list) => list.reduce((acc, val) => acc + val, 0);
546546
sum([1, 2, 3]) // 6
547547
```
548548

@@ -560,24 +560,24 @@ There's quite a bit of variance across the community but they often follow the f
560560
// functionName :: firstArgType -> secondArgType -> returnType
561561

562562
// add :: Number -> Number -> Number
563-
let add = (x) => (y) => x + y
563+
const add = (x) => (y) => x + y
564564

565565
// increment :: Number -> Number
566-
let increment = (x) => x + 1
566+
const increment = (x) => x + 1
567567
```
568568

569569
If a function accepts another function as an argument it is wrapped in parentheses.
570570

571571
```js
572572
// call :: (a -> b) -> a -> b
573-
let call = (f) => (x) => f(x)
573+
const call = (f) => (x) => f(x)
574574
```
575575

576576
The letters `a`, `b`, `c`, `d` are used to signify that the argument can be of any type. For this `map` it takes a function that transforms a value of some type `a` into another type `b`, an array of values of type `a`, and returns an array of values of type `b`.
577577

578578
```js
579579
// map :: (a -> b) -> [a] -> [b]
580-
let map = (f) => (list) => list.map(f)
580+
const map = (f) => (list) => list.map(f)
581581
```
582582

583583
## Union type

0 commit comments

Comments
 (0)