-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ls
89 lines (64 loc) · 1.4 KB
/
index.ls
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
require! daggy
module.exports = exports.ArrayT = (M)->
ArrayT = daggy.tagged \run
sequence = (m, ms)-->
ms.reduce do
(m1, m2)->
x <- m1.chain
xs <- m2.chain
m.of x ++ xs
m.of []
ArrayT.of = (x)->
ArrayT M.of [x]
# chain :: M [a] -> (a -> M [b]) -> M [b]
ArrayT::chain = (f)->
ArrayT @run.chain (o)->
sequence M, o.reduce do
(a, x)-> a ++ f x .run
[]
ArrayT::map = (f)->
@chain (a)-> ArrayT.of f a
ArrayT::ap = (a)->
@chain (f)-> a.map f
ArrayT::concat = (a)->
ArrayT sequence M, [@run, a.run]
ArrayT.empty = -> ArrayT M.of []
ArrayT::reduce = (f, i)->
@run.map (.reduce f, i)
ArrayT::reduce-right = (f, i)->
@run.map (.reduce-right f, i)
ArrayT::take = (n)->
ArrayT @run.map (.slice 0 n)
ArrayT::drop = (n)->
ArrayT @run.map (.slice n)
ArrayT::tail = ->
@drop 1
ArrayT::head = ->
@run.map (.0)
ArrayT::initial = ->
@take -1
ArrayT::last = ->
@run.map (a)-> a[a.length - 1]
ArrayT::reverse = ->
ArrayT @reduce-right do
(a, x)-> a ++ [x]
[]
ArrayT::len = ->
@run.map (.length)
ArrayT::filter = (f)->
ArrayT @run.map (.filter f)
ArrayT::reject = (f)->
ArrayT @run.map (.filter (not) . f)
ArrayT::find = (f)->
@filter f .head!
ArrayT::mkString = (j = '')->
@run.map (.join j)
ArrayT::every = (f)->
@reduce do
(a, x)-> a and f x
true
ArrayT::some = (f)->
@reduce do
(a, x)-> a or f x
false
ArrayT