@@ -1,17 +1,17 @@
1−module Lazy.Stream ( hd, tl
2− , mkStream , iterate, repeat, cycle
1+module Lazy.Stream ( head, tail
2+ , cons, cons', iterate, repeat, cycle
33 , map, apply, zip, zipWith, scanl
44 , take, drop, splitAt
5− , observeWhen
5+ , sampleOn
66 ) where
77
88{-| Library for Infinite Streams
99
1010# Observe
11−@docs hd, tl, observeWhen, take, drop, splitAt
11+@docs head, tail, sampleOn, take, drop, splitAt
1212
1313# Create
14−@docs mkStream, iterate, repeat, cycle
14+@docs cons', iterate, repeat, cycle
1515
1616# Transform
1717@docs map, apply, zip, zipWith, scanl
@@ -22,41 +22,41 @@ import Basics ((.), (<|), (-), id, fst, snd)
2222import open Lazy
2323import Signal ((<~), foldp, Signal)
2424
25−data Stream a = S (Lazy { hd : a, tl : Stream a })
25+data Stream a = S (Lazy (a, Stream a))
2626
27−hd : Stream a -> a
28−hd (S t) = (run t).hd
27+unS : Stream a -> (a, Stream a)
28+unS (S t) = force t
2929
30−tl : Stream a -> Stream a
31−tl (S t) = (run t).tl
30+head : Stream a -> a
31+head = fst . unS
3232
33−mkStream : (() -> { hd : a, tl : Stream a }) -> Stream a
34−mkStream = S . thunk
33+tail : Stream a -> Stream a
34+tail = snd . unS
35+
36+cons : a -> (() -> Stream a) -> Stream a
37+cons x txs = let mtxs = lazy txs in
38+ S . lazy <| \() ->
39+ (x, force mtxs)
40+
41+cons' : (() -> (a, Stream a)) -> Stream a
42+cons' = S . lazy
3543
3644iterate : (a -> a) -> a -> Stream a
37−iterate f x = mkStream <| \() ->
38− { hd = x
39− , tl = map f (iterate f x)
40− }
45+iterate f x = cons' <| \() ->
46+ (x, map f (iterate f x))
4147
4248repeat : a -> Stream a
4349repeat x = iterate id x
4450
4551cycle : a -> [a] -> Stream a
4652cycle x xs = let cycle' ys = case ys of
47− [] -> mkStream <| \() ->
48− { hd = x
49− , tl = cycle' xs }
50− (y :: ys) -> mkStream <| \() ->
51− { hd = y
52− , tl = cycle' ys }
53+ [] -> cons' <| \() ->
54+ (x, cycle' xs)
5355 in cycle' []
5456
5557map : (a -> b) -> Stream a -> Stream b
56−map f xs = mkStream <| \() ->
57− { hd = f (hd xs)
58− , tl = map f (tl xs)
59− }
58+map f xs = cons' <| \() ->
59+ (f (head xs), map f (tail xs))
6060
6161apply : Stream (a -> b) -> Stream a -> Stream b
6262apply fs xs = zipWith (<|) fs xs
@@ -65,16 +65,14 @@ zip : Stream a -> Stream b -> Stream (a, b)
6565zip = zipWith (\x y -> (x,y))
6666
6767zipWith : (a -> b -> c) -> Stream a -> Stream b -> Stream c
68−zipWith f xs ys = mkStream <| \() ->
69− { hd = f (hd xs) (hd ys)
70− , tl = zipWith f (tl xs) (tl ys)
71− }
68+zipWith f xs ys = cons' <| \() ->
69+ (f (head xs) (head ys),
70+ zipWith f (tail xs) (tail ys))
7271
7372scanl : (a -> b -> b) -> b -> Stream a -> Stream b
74−scanl f init xs = mkStream <| \() ->
75− { hd = init
76− , tl = scanl f (f (hd xs) init) (tl xs)
77− }
73+scanl f init xs = cons' <| \() ->
74+ (init,
75+ scanl f (f (head xs) init) (tail xs))
7876
7977take : Int -> Stream a -> [a]
8078take n xs = fst <| splitAt n xs
@@ -85,9 +83,9 @@ drop n xs = snd <| splitAt n xs
8583splitAt : Int -> Stream a -> ([a], Stream a)
8684splitAt n xs = case n of
8785 0 -> ([], xs)
88− n -> let (hds, end) = splitAt (n - 1) (tl xs)
89− in (hd xs :: hds, end)
86+ n -> let (heads, end) = splitAt (n - 1) (tail xs)
87+ in (head xs :: heads, end)
9088
91−observeWhen : Stream a -> Signal b -> Signal a
92−observeWhen str sig = let tls = foldp (\_ -> tl) str sig in
93− hd <~ tls
89+sampleOn : Stream a -> Signal b -> Signal a
90+sampleOn str sig = let tails = foldp (\_ -> tail) str sig in
91+ head <~ tails