-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlist.js
56 lines (45 loc) · 1.33 KB
/
list.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
53
54
55
56
const daggy = require('daggy')
const Either = require('data.either')
const Task = require('data.task')
const {Left, Right} = Either
const {Fix, ana, apo, cata, zygo, gcata, para, cataM, algProd, algCoProd, Mu, unfix} = require('./index')
const {inspect, id, compose} = require('./utils')
const ListF = daggy.taggedSum({Nil: [], Cons: ['x', 'xs']})
const {Nil, Cons} = ListF
ListF.prototype.inspect = function(f) {
return this.cata({
Nil: () => 'Nil',
Cons: (x, xs) => `Cons(${inspect(x)}, ${inspect(xs)})`
})
}
ListF.prototype.map = function(f) {
return this.cata({
Nil: () => Nil,
Cons: (x, xs) => Cons(x, f(xs))
})
}
ListF.prototype.traverse = function(of, f) {
return this.cata({
Nil: () => of(Nil),
Cons: (x, xs) => f(xs).map(ys => Cons(x, ys))
})
}
const from = xs =>
xs.reduce((acc, x) => cons(x, acc), nil)
const to = l =>
l.cata(t =>
t.cata({
Nil: () => [],
Cons: (x, xs) => [x].concat(xs)
}))
const nil = Fix(Nil)
const cons = (x, xs) => Fix(Cons(x, xs))
////console.log(para(tails, l1))
//const munil = Mu.embed(Nil)
//const mucons = (x, xs) => Mu.embed(Cons(x, xs))
//const arrToList_ = xs =>
// xs.reduce((acc, x) => mucons(x, acc), munil)
//const mul1 = arrToList_([1,2,3])
//console.log(mul1)
//console.log('sum', mul1.cata(sum))
module.exports = {nil, cons, Nil, Cons, from, to}