@@ -1,17 +1,17 @@
1−module Lazy.Stream ( hd, tl
1+module Lazy.Stream ( head, tail
2− , mkStream , iterate, repeat, cycle
2+ , cons, cons', iterate, repeat, cycle
3 , map, apply, zip, zipWith, scanl
3 , map, apply, zip, zipWith, scanl
4 , take, drop, splitAt
4 , take, drop, splitAt
5− , observeWhen
5+ , sampleOn
8{-| Library for Infinite Streams
8{-| Library for Infinite Streams
11−@docs hd, tl, observeWhen, take, drop, splitAt
11+@docs head, tail, sampleOn, take, drop, splitAt
14−@docs mkStream, iterate, repeat, cycle
14+@docs cons', iterate, repeat, cycle
16# Transform
16# Transform
17@docs map, apply, zip, zipWith, scanl
17@docs map, apply, zip, zipWith, scanl
@@ -22,41 +22,41 @@ import Basics ((.), (<|), (-), id, fst, snd)
22import open Lazy
22import open Lazy
23import Signal ((<~), foldp, Signal)
23import Signal ((<~), foldp, Signal)
25−data Stream a = S (Lazy { hd : a, tl : Stream a })
25+data Stream a = S (Lazy (a, Stream a))
27−hd : Stream a -> a
27+unS : Stream a -> (a, Stream a)
28−hd (S t) = (run t).hd
28+unS (S t) = force t
30−tl : Stream a -> Stream a
30+head : Stream a -> a
31−tl (S t) = (run t).tl
31+head = fst . unS
33+tail : Stream a -> Stream a
36+cons : a -> (() -> Stream a) -> Stream a
37+cons x txs = let mtxs = lazy txs in
33−mkStream : (() -> { hd : a, tl : Stream a }) -> Stream a
41+cons' : (() -> (a, Stream a)) -> Stream a
34−mkStream = S . thunk
42+cons' = S . lazy
36iterate : (a -> a) -> a -> Stream a
44iterate : (a -> a) -> a -> Stream a
37−iterate f x = mkStream <| \() ->
45+iterate f x = cons' <| \() ->
39− , tl = map f (iterate f x)
46+ (x, map f (iterate f x))
42repeat : a -> Stream a
48repeat : a -> Stream a
43repeat x = iterate id x
49repeat x = iterate id x
45cycle : a -> [a] -> Stream a
51cycle : a -> [a] -> Stream a
46cycle x xs = let cycle' ys = case ys of
52cycle x xs = let cycle' ys = case ys of
47− [] -> mkStream <| \() ->
53+ [] -> cons' <| \() ->
49− , tl = cycle' xs }
54+ (x, cycle' xs)
50− (y :: ys) -> mkStream <| \() ->
53 in cycle' []
55 in cycle' []
55map : (a -> b) -> Stream a -> Stream b
57map : (a -> b) -> Stream a -> Stream b
56−map f xs = mkStream <| \() ->
58+map f xs = cons' <| \() ->
58− , tl = map f (tl xs)
59+ (f (head xs), map f (tail xs))
61apply : Stream (a -> b) -> Stream a -> Stream b
61apply : Stream (a -> b) -> Stream a -> Stream b
62apply fs xs = zipWith (<|) fs xs
62apply fs xs = zipWith (<|) fs xs
@@ -65,16 +65,14 @@ zip : Stream a -> Stream b -> Stream (a, b)
65zip = zipWith (\x y -> (x,y))
65zip = zipWith (\x y -> (x,y))
67zipWith : (a -> b -> c) -> Stream a -> Stream b -> Stream c
67zipWith : (a -> b -> c) -> Stream a -> Stream b -> Stream c
68−zipWith f xs ys = mkStream <| \() ->
68+zipWith f xs ys = cons' <| \() ->
69− { hd = f (hd xs) (hd ys)
69+ (f (head xs) (head ys),
70− , tl = zipWith f (tl xs) (tl ys)
70+ zipWith f (tail xs) (tail ys))
73scanl : (a -> b -> b) -> b -> Stream a -> Stream b
72scanl : (a -> b -> b) -> b -> Stream a -> Stream b
74−scanl f init xs = mkStream <| \() ->
73+scanl f init xs = cons' <| \() ->
75− { hd = init
74+ (init,
76− , tl = scanl f (f (hd xs) init) (tl xs)
75+ scanl f (f (head xs) init) (tail xs))
79take : Int -> Stream a -> [a]
77take : Int -> Stream a -> [a]
80take n xs = fst <| splitAt n xs
78take n xs = fst <| splitAt n xs
@@ -85,9 +83,9 @@ drop n xs = snd <| splitAt n xs
85splitAt : Int -> Stream a -> ([a], Stream a)
83splitAt : Int -> Stream a -> ([a], Stream a)
86splitAt n xs = case n of
84splitAt n xs = case n of
87 0 -> ([], xs)
85 0 -> ([], xs)
88− n -> let (hds, end) = splitAt (n - 1) (tl xs)
86+ n -> let (heads, end) = splitAt (n - 1) (tail xs)
89− in (hd xs :: hds, end)
87+ in (head xs :: heads, end)
91−observeWhen : Stream a -> Signal b -> Signal a
89+sampleOn : Stream a -> Signal b -> Signal a
92−observeWhen str sig = let tls = foldp (\_ -> tl) str sig in
90+sampleOn str sig = let tails = foldp (\_ -> tail) str sig in
93− hd <~ tls
91+ head <~ tails