-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path9-soc.js
More file actions
27 lines (24 loc) · 600 Bytes
/
9-soc.js
File metadata and controls
27 lines (24 loc) · 600 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
'use strict';
const adder = (initial) => {
let value = initial;
const add = (delta) => {
value += delta;
};
const get = () => value;
const map = (fn) => adder(fn(value));
const set = (x) => {
value = x;
};
return { add, get, map, set };
};
// Usage
const counter = adder(10);
console.log({ step1: counter.get() });
counter.add(5);
console.log({ step2: counter.get() });
const twice = (a) => a * 2;
const counter2 = counter.map(twice);
console.log({ counter2: counter2.get() });
console.log({ step3: counter.get() });
counter.set(50);
console.log({ step4: counter.get() });