@@ -1,275 +0,0 @@
1−module Lazy.Stream ( head, tail
2− , cons, cons', iterate, repeat, cycle
3− , map, apply, zip, zipWith, scanl
6− , filter, takeWhile, dropWhile, splitWith
9−{-| This library is for creating and manipulating infinite streams
12−@docs cons, cons', repeat, cycle, iterate
15−@docs head, tail, sampleOn, take, takeWhile, drop, dropWhile, splitAt, splitWith
18−@docs map, zip, zipWith, scanl, filter, apply
22−import Basics ((.), (<|), (-), id, fst, snd)
24−import Signal ((<~), foldp, Signal)
26−data Stream a = S (Lazy (a, Stream a))
28−unS : Stream a -> (a, Stream a)
31−{-| Get the first element of a stream, called the `head`.
40−{-| Drop the `head` of a stream, leaving you with the `tail`.
47−tail : Stream a -> Stream a
54−ones = cons 1 (\() -> ones)
57−cons : a -> (() -> Stream a) -> Stream a
58−cons x txs = let mtxs = lazy txs in
62−{-| Create a stream that is slightly more lazy. Notice that the
63−head of the stream is defined within the thunk, so its evaluation
64−is delayed. This is nice when each element of the stream is costly to compute.
68−ones = cons' (\_ -> (1,ones))
71−cons' : (() -> (a, Stream a)) -> Stream a
74−{-| Iteratively apply a function to a value:
77−-- x, f x, f (f x), f (f (f x)), ...
81−powersOf2 = iterate (\n -> n^2) 2
84−iterate : (a -> a) -> a -> Stream a
86− cons' <| \_ -> (x, iterate f (f x))
88−{-| Repeat a value infinitely:
95−repeat : a -> Stream a
96−repeat x = let go = cons x <| \() -> go
99−{-| Infinitely cycle through a list, where the head and tail of
100−the list are given separately to ensure that no one tries to cycle
104−-- "Alice", "Bob", "Alice", "Bob", ...
105−cycle "Alice" ["Bob"]
108−cycle : a -> [a] -> Stream a
110− let cycle' ys = case ys of
112− (y :: ys) -> cons y (\_ -> cycle' ys)
113− go = cons' <| \_ -> (x, cycle' xs)
116−{-| Apply a function to every element of a Stream.
120−twos = map (\n -> n + 1) ones
123−map : (a -> b) -> Stream a -> Stream b
125− cons' <| \_ -> (f (head xs), map f (tail xs))
127−{-| Pairwise apply a stream of functions to a stream of arguments.
128−When paired with `map`, this can be used to emulate `zipWith` over *n* streams.
131−zipWith3 : (a -> b -> c -> d) -> Stream a -> Stream b -> Stream c -> Stream d
132−zipWith3 f xs ys zs = f `S.map` xs `S.apply` ys `S.apply` zs
135−apply : Stream (a -> b) -> Stream a -> Stream b
136−apply fs xs = zipWith (<|) fs xs
138−{-| Combine two streams, putting them into tuples pairwise:
141−-- (1,2), (1,4), (1,8), (1,16), ...
142−pairs = zip ones powersOf2
145−zip : Stream a -> Stream b -> Stream (a,b)
146−zip = zipWith (\x y -> (x,y))
148−{-| Combine two streams, applying the given function pairwise:
152−zipWith (+) ones powersOf2
155−zipWith : (a -> b -> c) -> Stream a -> Stream b -> Stream c
156−zipWith f xs ys = cons' <| \() ->
157− (f (head xs) (head ys),
158− zipWith f (tail xs) (tail ys))
160−{-| Scan over a stream from the left, building an infinite stream of reductions.
164−runningTotal = scanl (+) 0 ones
167−scanl : (a -> b -> b) -> b -> Stream a -> Stream b
169− cons' <| \() -> (init, scanl f (f (head xs) init) (tail xs))
171−{-| Take the first *n* elements of a stream
174−take 5 powersOf2 == [2,4,8,16,32]
177−take : Int -> Stream a -> [a]
178−take n xs = fst <| splitAt n xs
180−{-| Drop the first *n* elements of a stream
183−-- 64, 128, 256, 512, ...
187−drop : Int -> Stream a -> Stream a
188−drop n xs = snd <| splitAt n xs
190−{-| Combination of `take` and `drop`
193−splitAt n xs == (take n xs, drop n xs)
196−splitAt : Int -> Stream a -> ([a], Stream a)
197−splitAt n xs = case n of
199− n -> let (heads, end) = splitAt (n - 1) (tail xs)
200− in (head xs :: heads, end)
202−{-| Turn a stream into a signal of the elements of the stream,
203−advancing through the stream whenever an event in the given signal is
206−sampleOn : Signal b -> Stream a -> Signal a
207−sampleOn sig str = let tails = foldp (\_ -> tail) str sig in
210−{-| Filter the elements of a Stream according to a predicate.
214−filter isOdd naturals
216−-- Bad! Infinite loop!
220−This can infinite loop if no more elements in the stream satisfy the predicate!
222−filter : (a -> Bool) -> Stream a -> Stream a
223−filter pred xs = cons' <| (\() ->
224− let (hd, tl) = unS xs in
226− True -> (hd, filter pred tl)
227− False -> unS <| filter pred tl)
229−{-| Take values from the stream for as long as the predicate holds.
230−This can infinite loop if all elements of the stream satisfy the predicate!
233−takeWhile (\n -> n < 10) powersOf2 == [2,4,8]
235−-- Bad! Infinite loop!
236−takeWhile isEven powersOf2
239−This can infinite loop if all elements of the stream satisfy the predicate!
241−takeWhile : (a -> Bool) -> Stream a -> [a]
242−takeWhile pred xs = fst <| splitWith pred xs
244−{-| Drop values from the stream as long as the predicate holds.
247−-- 16, 32, 64, 128, ...
248−dropWhile (\n -> n < 10) powersOf2
250−-- Bad! Infinite loop!
251−dropWhile isEven powersOf2
254−This can infinite loop if all elements of the stream satisfy the predicate!
256−dropWhile : (a -> Bool) -> Stream a -> Stream a
257−dropWhile pred xs = snd <| splitWith pred xs
259−{-| Combination of `takeWhile` and `dropWhile`. Split a stream when
260−the predicate no longer holds.
263−splitWith pred xs == (takeWhile pred xs, dropWhile pred xs)
265−splitWith (\n -> n < 10) powersOf2 == ([2,4,8], ...)
268−This can infinite loop if all elements satisfy the predicate!
270−splitWith : (a -> Bool) -> Stream a -> ([a], Stream a)
271−splitWith pred xs = let (hd, tl) = unS xs in
273− True -> let (taken, dropped) = splitWith pred tl
274− in (hd :: taken, dropped)