Skip to content
9 changes: 4 additions & 5 deletions ch-11.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ const fold = f => acc => ([x, ...xs]) =>
? acc
: f(fold(f) (acc) (xs)) (x);


const map = f => fold(acc => x =>
arrCons_(acc) (f(x)))
([]);

const sqr = x => x * x;

const xs = [1,2,3];
const xs = [1, 2, 3];

const main = map(sqr);

Expand All @@ -55,7 +54,7 @@ const take = n => ([x, ...xs]) =>
? []
: [x].concat(take(n - 1) (xs));
// ^^^^^^

const xs = Array(1e5).fill(1).map((x, i) => x + i);

take(200) (xs); // incredible inefficient
Expand All @@ -75,7 +74,7 @@ const take = n => xs =>
n < 0
? []
: push(xs[n]) (take(n - 1) (xs));

const xs = Array(1e5).fill(1).map((x, i) => x + i);

take(200) (xs); // quite efficient
Expand Down Expand Up @@ -104,7 +103,7 @@ const map = f => xs => fold(acc => x =>

const sqr = x => x * x;

const xs = [1,2,3];
const xs = [1, 2, 3];

const main = map(sqr);

Expand Down
8 changes: 4 additions & 4 deletions ch-2.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ o.foo = false; // state
```
### Variables and reassignments

Although the term variable is used in functional programming and math I prefer to avoid it because it regularly causes confusion: There are no variables in FP at least not those as they are understood in imperative programming. All we have at our disposal are immutable name bindings, that is you cannot reassign another value to an existing name.
Although the term variable is used in functional programming and math I prefer to avoid it because it regularly causes confusion: There are no variables in FP at least not those as they are understood in imperative programming. All we have at our disposal are immutable name bindings, that is you cannot reassign another value to an existing name.

Reassignments are banned in functional programming since they would violate referential transparency:

```javascript
let = y = 2;
let y = 2;
const sub2 = x => x – y

sub2(2); // 0
Expand Down Expand Up @@ -173,9 +173,9 @@ The functional way of managing state may be hard to digest for imperative progra

```javascript
const compState = f => g => h => x => s => {
const [x_, s_] = h(x) (s);
const [x_, s_] = h(x) (s),
[x__, s__] = g(x_) (s_);

return f(x__) (s__);
};

Expand Down
8 changes: 4 additions & 4 deletions ch-3.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ Since every curried function expects a single argument without exception there i
const comp = f => g => x => f(g(x));
const add = x => y => x + y;
const inc = x => x + 1;
const main = comp(add) (inc); (A)
const main2 = comp(add) (add); (B)
const main = comp(add) (inc); // (A)
const main2 = comp(add) (add); // (B)

comp(add) (inc) (1) (2); // 4
comp(add) (inc) (1) (2); // 4
comp(add) (add) (1) (2); // "y => x + y2"
```
[run code](https://repl.it/repls/NotableButterySection)
Expand Down Expand Up @@ -175,7 +175,7 @@ sub2(3); // 1 (as expected)
An alternative to `flip` is `appr`, which mimics operators in infix position:

```javascript
const appl = (x, f) => y => // left section (unnecessary)
const appl = (x, f) => y => // left section (unnecessary)
f(x) (y);

const infix = (x, f, y) => x => // infix position
Expand Down
12 changes: 7 additions & 5 deletions ch-5.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const foo = x => [
x + x,
x - x,
x * x];

foo(2 + 3);
```
The invocation of `foo` triggers the evaluation of `2 + 3` only once, even though it is used six times.
Expand Down Expand Up @@ -95,7 +95,7 @@ Can we defer the function composition even further?
const compk = f => g => x => k =>
k(f(g(x)));
// ^^^^^^^ deferring effect

const inc = x => x + 1;

const sqr = x => x * x;
Expand Down Expand Up @@ -132,6 +132,8 @@ Nullary functions are also referred to as thunks in Javascript. They are infecti
```javascript
// simplified version

const THUNK = "thunk";

class LazyProxy {
constructor(f) {
this.f = f;
Expand All @@ -157,7 +159,7 @@ class LazyProxy {

const thunk = f =>
new Proxy(f, new LazyProxy(f));

const log = x =>
(console.log("log", x), x);

Expand Down Expand Up @@ -192,7 +194,7 @@ const foo = x => [
x + x, // x is needed to process the operation
x - x,
mul(x) (x)]; // the result of the multiplication is not needed yet

const main = foo(add(2) (3)); // logs "evaluating x to 5" only once and yields [10, 0, thunk]

main.map(x => -x); // forces evaluation of the thunk and yields [-10, -0, -25]
Expand Down Expand Up @@ -252,7 +254,7 @@ const fact = fix(go => n =>
n === 0
? 1
: n * go(n - 1));

fact(5); // 120
```
[run code](https://repl.it/repls/MurkyDetailedAutosketch)
Expand Down