on master help sign in
Split Streams into separate module and add apply (a la <*>)
commit9f98ec9b191fcb0529e4f47e8e7a4b5a91bd4c69 authorMax New <maxsnew@gmail.com> dateMon 9 Dec 2013, 00:01 (13 years ago) parent614c8c5
2 files changed+72−48
libraries/Lazy.elm+4−48 libraries/Lazy/Stream.elm+68
libraries/Lazy.elm+4−48
@@ -1,12 +1,10 @@
1+module Lazy ( run, thunk
2+ ) where
13
2−module Lazy where
3−
4−{-| Library for efficient Lazy evaluation and Infinite Streams.
4+{-| Library for efficient Lazy evaluation.
55
66-}
77
8−import Basics ((-))
9−import Signal ((<~), foldp, Signal)
108import Native.Lazy
119
1210data Lazy a = L { run : () -> a }
@@ -16,47 +14,5 @@ thunk : (() -> a) -> Lazy a
1614thunk t = L { run = (Native.Lazy.thunk t) }
1715
1816run : Lazy a -> a
19−run l = case l of L r -> r.run ()
20−
21−data Stream a = S { hd : a, tl : Lazy (Stream a) }
22−
23−hd : Stream a -> a
24−hd (S r) = r.hd
25−
26−tl : Stream a -> Stream a
27−tl (S r) = run (r.tl)
28−
29−repeat : a -> Stream a
30−repeat x = S { hd = x, tl = thunk (\() -> repeat x) }
31−
32−cycle : a -> [a] -> Stream a
33−cycle x xs = let cycle' ys = case ys of
34− [] -> S { hd = x, tl = thunk (\() -> cycle' xs) }
35− (y :: ys) -> S { hd = y, tl = thunk (\() -> cycle' ys) }
36− in cycle' []
37−
38−map : (a -> b) -> Stream a -> Stream b
39−map f xs = S { hd = f (hd xs)
40− , tl = thunk (\() -> map f (tl xs))
41− }
42−
43−zip : Stream a -> Stream b -> Stream (a, b)
44−zip = zipWith (\x y -> (x,y))
45−
46−zipWith : (a -> b -> c) -> Stream a -> Stream b -> Stream c
47−zipWith f xs ys = S { hd = f (hd xs) (hd ys)
48− , tl = thunk (\() -> zipWith f (tl xs) (tl ys)) }
49−
50−take : Int -> Stream a -> [a]
51−take n xs = case n of
52− 0 -> []
53− n -> hd xs :: take (n - 1) (tl xs)
54−
55−iterate : (a -> a) -> a -> Stream a
56−iterate f x = S { hd = x
57− , tl = thunk (\() -> map f (iterate f x))
58− }
17+run (L r) = r.run ()
5918
60−observeWhen : Stream a -> Signal b -> Signal a
61−observeWhen str sig = let tls = foldp (\_ -> tl) str sig in
62− hd <~ tls
libraries/Lazy/Stream.elmnew file+68
@@ -0,0 +1,68 @@
1+module Lazy.Stream ( hd, tl, mkStream, repeat, cycle
2+ , map, apply, zip, zipWith, iterate
3+ , take, observeWhen
4+ ) where
5+
6+{-| Library for Infinite Streams -}
7+
8+import Basics ((.), (<|), (-))
9+import open Lazy
10+import Signal ((<~), foldp, Signal)
11+
12+data Stream a = S (Lazy { hd : a, tl : Stream a })
13+
14+hd : Stream a -> a
15+hd (S t) = (run t).hd
16+
17+tl : Stream a -> Stream a
18+tl (S t) = (run t).tl
19+
20+mkStream : (() -> { hd : a, tl : Stream a }) -> Stream a
21+mkStream = S . thunk
22+
23+repeat : a -> Stream a
24+repeat x = mkStream <| \() ->
25+ { hd = x, tl = repeat x }
26+
27+cycle : a -> [a] -> Stream a
28+cycle x xs = let cycle' ys = case ys of
29+ [] -> mkStream <| \() ->
30+ { hd = x
31+ , tl = cycle' xs }
32+ (y :: ys) -> mkStream <| \() ->
33+ { hd = y
34+ , tl = cycle' ys }
35+ in cycle' []
36+
37+map : (a -> b) -> Stream a -> Stream b
38+map f xs = mkStream <| \() ->
39+ { hd = f (hd xs)
40+ , tl = map f (tl xs)
41+ }
42+
43+apply : Stream (a -> b) -> Stream a -> Stream b
44+apply fs xs = zipWith (<|) fs xs
45+
46+zip : Stream a -> Stream b -> Stream (a, b)
47+zip = zipWith (\x y -> (x,y))
48+
49+zipWith : (a -> b -> c) -> Stream a -> Stream b -> Stream c
50+zipWith f xs ys = mkStream <| \() ->
51+ { hd = f (hd xs) (hd ys)
52+ , tl = zipWith f (tl xs) (tl ys)
53+ }
54+
55+iterate : (a -> a) -> a -> Stream a
56+iterate f x = mkStream <| \() ->
57+ { hd = x
58+ , tl = map f (iterate f x)
59+ }
60+
61+take : Int -> Stream a -> [a]
62+take n xs = case n of
63+ 0 -> []
64+ n -> hd xs :: take (n - 1) (tl xs)
65+
66+observeWhen : Stream a -> Signal b -> Signal a
67+observeWhen str sig = let tls = foldp (\_ -> tl) str sig in
68+ hd <~ tls