-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ls
121 lines (100 loc) · 2.05 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
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
export class Stream
(@generator)~>
#map :: Stream a -> (a -> b) -> Steam b
map: (f)->
@chain (c)->
Stream.of f c
#chain :: Stream a -> (a -> Stream b) -> Steam b
chain: (f)->
Stream (next,end)~>
@generator do
(chunk)~> (f chunk).generator next, (->)
end
#ap :: Stream (a -> b) -> Stream a -> Stream b
ap: (o)->
@chain (f)->
o.map f
# concat :: Stream a -> Stream a -> Stream a
concat: (other)->
Stream (next,end)~>
@generator next, -> other.generator next, end
@from-readable = (readable)->
Stream (next, end)->
readable.on \data next
readable.on \end end
@of = (obj)->
Stream (next,end)->
next obj if obj?
end!
@from-array = (arr)->
Stream (next,end)->
arr.for-each next
end!
@empty = -> Stream.of!
pipe: (dest, options = {end:true})->
@generator do
(chunk)->
do function write
unless dest.write chunk
dest.once \drain write
-> dest.end! unless dest._isStdio or not options.end
to-string: -> "[object Stream]"
take: (n)->
taken = 0
@chain (chunk)->
| taken >= n => Stream.empty!
| otherwise => taken++; Stream.of chunk
drop: (n)->
dropped = 0
@chain (chunk)->
| dropped < n => dropped++; Stream.empty!
| otherwise => Stream.of chunk
to-charstream: ->
@chain (str)->
Stream.from-array str.split ''
scanl: (acc, f)->
Stream (next,end)~>
@generator do
(chunk)-> next acc := f acc,chunk
end
scanl1: (f)->
Stream (next,end)~>
init = true
var acc
@generator do
(chunk)->
next acc := if init
init := false
chunk
else f acc,chunk
end
scan: ::scanl
scan1: ::scanl1
foldl: (acc,f)->
Stream (next,end)~>
@generator do
(chunk)-> acc := f acc,chunk
->
next acc
end!
foldl1: (f)->
Stream (next,end)~>
init = true
var acc
@generator do
(chunk)->
acc := if init
init := false
chunk
else f acc,chunk
->
next acc
end!
fold: ::foldl
fold1: ::foldl1
length: ->
@map -> 1
.foldl 0,(+)
zip-with-index: ->
i = 0
@map (c)-> [c, i++]