-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chapter 1.js
52 lines (45 loc) · 1.39 KB
/
Chapter 1.js
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const test = require('tape');
const fc = require('fast-check');
/**
* Identity
* @param {*} x - anything
* @return {*} x
*/
const id = x => x;
/**
* Compose - combines all functions from right to left
* @param {...function(*): *} f - a list of single argument function
* @return {function(*): *} - a single argument function
*/
const compose = (...f) => x => f.reverse().reduce((x, f) => f(x), x);
test('identity', assert => {
assert.plan(1);
assert.doesNotThrow(() => {
fc.assert(
fc.property(fc.anything(), x => x === id(x))
)
}, 'should be universally polymorphic')
});
test('composition', assert => {
const adder = x => y => x + y;
const multiplier = x => y => x * y;
assert.plan(3);
assert.doesNotThrow(() => {
fc.assert(
fc.property(fc.integer(), fc.integer(), (x, y) =>
compose(adder(x), multiplier(y))(x) === adder(x)(multiplier(y)(x)))
)
}, 'should compose from right to left');
assert.doesNotThrow(() => {
fc.assert(
fc.property(fc.integer(), fc.integer(), (x, y) =>
compose(adder(x), multiplier(y), adder(y))(x) === compose(compose(adder(x), multiplier(y)), adder(y))(x))
)
}, 'should be associative')
assert.doesNotThrow(() => {
fc.assert(
fc.property(fc.integer(), fc.integer(), (x, y) =>
compose(adder(x), id)(y) === compose(id, adder(x))(y))
)
}, 'should contain a unit of composition')
})