-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStream.elm
374 lines (274 loc) · 7.34 KB
/
Stream.elm
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
module Stream
exposing
( Stream
, concatMap
, append
, cons
, continue
, create
, empty
, filter
, fromList
, map
, range
, singleton
, stop
, take
, takeWhile
, toList
)
{-| Fast and simple stream library for Elm. Create streams of data or flatten
multiple operations over lists.
# Stream Type
@docs Stream
# Use Streams with Lists
@docs fromList, toList
# Create Streams
@docs singleton, empty, range, create, continue, stop
# Add to Streams
@docs cons, append
# Transform Streams
@docs map, filter, take, takeWhile, concatMap
-}
{-| Represent a stream with a thunk. Streams let you efficiently and lazily
transform values. Streams flatten operations like [`map`](#map) and
[`filter`](#filter) so you don't have to iterate through a stream multiple
times like a list.
Stream.range 1 1000
|> Stream.map (\n -> n * 2)
|> Stream.filter (\n -> n > 10)
|> Stream.take 3
|> Stream.toList == [12, 14, 16]
You can use streams with lists of data too.
[1, 2, 3, 4, 5]
|> Stream.fromList
|> Stream.map (\n -> 2 ^ n)
|> Stream.filter (\n -> n > 8)
|> Stream.toList == [16, 32]
-}
type alias Stream a =
() -> StreamElement a
type StreamElement a
= Cons a (Stream a)
| Nil
type Next a
= Continue a
| Stop
{-| Create a stream from a list.
fromList [1, 2, 3]
-}
fromList : List a -> Stream a
fromList list () =
case list of
[] ->
Nil
x :: xs ->
Cons x (fromList xs)
{-| Convert a stream into a list. Evaluates every value in a stream. Be careful
with infinite streams, so use [`take`](#take) before calling.
[1, 2, 3]
|> fromList
|> toList == [1, 2, 3]
-}
toList : Stream a -> List a
toList stream =
let
toList_ : StreamElement a -> List a -> List a
toList_ streamElement list =
case streamElement of
Cons value nextStream ->
toList_ (nextStream ()) (value :: list)
Nil ->
list
in
[]
|> toList_ (stream ())
|> List.reverse
{-| A stream with no values.
empty
|> toList == []
-}
empty : Stream a
empty () =
Nil
{-| Create a stream with a single value.
singleton 42
|> toList == [42]
-}
singleton : a -> Stream a
singleton value =
cons value empty
{-| Add a value to the beginning of a stream.
cons (singleton 1) (singleton 2)
|> toList == [1, 2]
-}
cons : a -> Stream a -> Stream a
cons value stream () =
Cons value stream
{-| Combine two streams.
append (range 1 3) (range 4 6)
|> toList == [1, 2, 3, 4, 5, 6]
-}
append : Stream a -> Stream a -> Stream a
append stream1 stream2 () =
case stream1 () of
Cons value nextStream ->
Cons value (append nextStream stream2)
Nil ->
stream2 ()
{-| Create a stream of numbers with each number increasing by one. Provide the
starting and ending number as arguments.
range 1 10
|> toList == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
-}
range : Int -> Int -> Stream Int
range start end =
if start > end then
empty
else
create
(\n ->
if n < end then
continue (n + 1)
else
stop
)
start
{-| Create a stream with a generator function to produce
each value based on the previous value. Return the next value wrapped in
[`continue`](#continue) or stop the stream by returning [`stop`](#stop).
Provide the initial value as the second argument to `create`.
naturalNumbers =
create (\n -> continue (n + 1)) 1
upTo5 =
create
(\n ->
if n < 5 then
continue (n + 1)
else
stop
)
1
-}
create : (a -> Next a) -> a -> Stream a
create generator initialValue () =
let
create_ : Next a -> StreamElement a
create_ next =
case next of
Continue value ->
Cons value (\() -> create_ (generator value))
Stop ->
Nil
in
create_ (continue initialValue)
{-| Used with [`create`](#create) to continue the stream with a new value.
naturalNumbers =
create (\n -> continue (n + 1)) 1
-}
continue : a -> Next a
continue =
Continue
{-| Used with [`create`](#create) to stop the stream.
upTo5 =
create
(\n ->
if n < 5 then
continue (n + 1)
else
stop
)
1
-}
stop : Next a
stop =
Stop
{-| Transform every value in a stream with a function.
[1, 2, 3]
|> fromList
|> map (\n -> n * 2)
|> toList == [2, 4, 6]
-}
map : (a -> b) -> Stream a -> Stream b
map f stream () =
case stream () of
Cons value nextStream ->
Cons (f value) (map f nextStream)
Nil ->
Nil
{-| Keep values that return `True` for the provided function.
range 1 10
|> filter (\n -> n > 7)
|> toList == [8, 9, 10]
-}
filter : (a -> Bool) -> Stream a -> Stream a
filter f stream () =
case stream () of
Cons value nextStream ->
if f value then
Cons value nextStream
else
filter f nextStream ()
Nil ->
Nil
{-| Take only up to *n* values from the stream. Useful for consuming infinite
streams.
naturalNumbers =
create (\n -> continue (n + 1)) 1
naturalNumbers
|> take 3
|> toList == [1, 2, 3]
-}
take : Int -> Stream a -> Stream a
take n stream () =
if n <= 0 then
Nil
else
case stream () of
Cons value nextStream ->
Cons value (take (n - 1) nextStream)
Nil ->
Nil
{-| Take values as long as the predicate function returns true.
range 1 10
|> takeWhile (\n -> n < 6)
|> toList == [1, 2, 3, 4, 5]
-}
takeWhile : (a -> Bool) -> Stream a -> Stream a
takeWhile predicate stream () =
case stream () of
Cons value nextStream ->
if predicate value then
Cons value (takeWhile predicate nextStream)
else
Nil
Nil ->
Nil
{-| Map values to streams and flatten the resulting streams.
-- Convert list of words to stream of letters
["hello", "there"]
|> fromList
|> concatMap (\word -> word |> String.split "" |> fromList)
|> toList == ["h", "e", "l", "l", "o", "t", "h", "e", "r", "e"]
-- Flatten inner stream ranges
range 1 3
|> concatMap (\n -> range n (n + 2))
|> toList == [1, 2, 3, 2, 3, 4, 3, 4, 5]
-- Skip values with `empty`
range 1 10
|> concatMap
(\n ->
if n < 6 then
empty
else
singleton n
)
|> toList [6, 7, 8, 9, 10]
-}
concatMap : (a -> Stream b) -> Stream a -> Stream b
concatMap f stream () =
case stream () of
Cons value nextStream ->
append (f value) (concatMap f nextStream) ()
Nil ->
Nil