-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
137 lines (122 loc) · 3.02 KB
/
index.js
File metadata and controls
137 lines (122 loc) · 3.02 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const successFn = (counter, msg) => console.log(`${counter}. ${msg} ✅`)
const errorFn = (testCounter, msg, result1, result2, res) => {
console.log(`${testCounter}. ${msg} ❌`)
console.log(` ❌ Got: ${result1} === ${result2}`)
console.log(` -> Expected ${res}`)
}
let testCounter = 0
const test = msg => (f, j) => res => (...args) => {
const result1 = f(...args)
const result2 = j(...args)
testCounter++
const success =
String(result1) === String(res) && String(result2) === String(res)
success
? successFn(testCounter, msg)
: errorFn(testCounter, msg, result1, result2, res)
}
// Identity monad
const Id = x => ({
x,
map: f => Id(f(x)),
fold: f => f(x),
chain: f => f(x),
concat: o => Id(x.concat(o.x)),
toString: () => `Id(${x})`,
})
Id.of = x => Id(x)
const Left = x => ({
x,
map: _ => Left(x),
fold: f => f(x),
chain: _ => Left(x),
concat: o => Left(x.concat(o.x)),
toString: () => `Left(${x})`,
})
Left.of = x => Left(x)
const Right = x => ({
x,
map: f => Right(f(x)),
fold: (_, g) => g(x),
chain: f => f(x),
concat: o => Right(x.concat(o.x)),
toString: () => `Right(${x})`,
})
Right.of = x => Right(x)
const Either = x => ({
x,
map: f => Either(f(x)),
fold: f => f(x),
chain: f => f(x),
concat: o => Either(x.concat(o.x)),
toString: () => `Either(${x})`,
})
Either.of = x => Either(x)
const fromNullable = (x, err) => (x != null ? Right(x) : Left(err))
const tryCatch = f => {
try {
return Right(f())
} catch (e) {
return Left(e)
}
}
// ---- Function modelling
// Functors: they can only map
const Fn = run => ({
run,
chain: f => Fn(x => f(run(x)).run(x)),
map: f => Fn(x => f(run(x))),
concat: other => Fn(x => run(x).concat(other.run(x))),
})
Fn.ask = Fn(x => x)
Fn.of = x => Fn(() => x)
// ---- Contravariant functors
// They have the ability to contramap
const Endo = run => ({
// Called after endomorphism
// Only works with types a -> a
// Will compose as its concatenation behaviour
// It is not a functor because using map we could change it's type
// therefore, it doesn't have the `map` method
run,
concat: other => Endo(x => run(other.run(x))),
contramap: fn => Endo(x => run(fn(x))),
})
Endo.of = x => Endo(x)
Endo.empty = () => Endo(x => x)
// Types equivalences
// (acc, x) -> acc
// (x, acc) -> acc
// x -> acc -> acc
// x -> Endo(acc -> acc)
// Fn(x -> Endo(acc -> acc))
const Reducer = run => ({
// (acc, x) -> acc
run,
concat: other => Reducer((acc, x) => other.run(run(acc, x), x)),
contramap: f => Reducer((acc, x) => run(acc, f(x))),
})
Reducer.empty = () => Reducer(() => {})
const Pred = run => ({
run,
concat: other => Pred(x => run(x) && other.run(x)),
contramap: fn => Pred(x => run(fn(x))),
})
Pred.of = run => Pred(run)
// ---- Profunctors
// They can contramap and map, and therefore can change the
// 'input' and the 'output' as a connector functors
// ---- Exports
const types = {
Endo,
Fn,
Id,
Left,
Right,
fromNullable,
test,
tryCatch,
Reducer,
Pred,
}
module.exports = types