-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
35 lines (27 loc) · 939 Bytes
/
index.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
// Misc
export const last = as => as[as.length - 1];
export const lastN = n => as => as.slice(as.length - n);
export const range = n => [...Array(n).keys()];
// Algebra
export const Nil = [];
export const Cons = x => xs => [x, ...xs];
export const match = ({ Nil, Cons }) => xs =>
xs.length === 0 ? Nil : Cons(xs[0])(xs.slice(1));
// Identity
export const is = x => Array.isArray(x);
// Functor
export const map = f => as => as.map(f);
// Applicative
export const of = x => [x];
export const lift2 = f => a1 => a2 =>
a1 |> foldl(b => a => a2 |> map(f(a)) |> append(b))(empty);
// Monoid
export const empty = [];
export const append = a1 => a2 => [...a1, ...a2];
// Monad
export const chain = f => foldl(b => a => f(a) |> append(b))(empty);
// Foldable
export const foldl = f => z => as => as.reduce((p, c) => f(p)(c), z);
// Traversable
export const sequence = A =>
foldl(A.lift2(b => a => of(a) |> append(b)))(A.of(empty));