on master help sign in
Lazy/Stream API changes
commit46f3fa1b2c5509b8c61d143a290a2a3468881994 authorMax New <maxsnew@gmail.com> dateMon 9 Dec 2013, 15:16 (13 years ago) parent5bb782a
3 files changed+51−53
libraries/Lazy.elm+12−12 libraries/Lazy/Stream.elm+37−39 libraries/Native/Lazy.js+2−2
libraries/Lazy.elm+12−12
@@ -1,4 +1,4 @@
1−module Lazy ( run, thunk, map, apply, bind
1+module Lazy ( force, lazy, map, apply, bind
22 ) where
33
44{-| Library for efficient Lazy evaluation.
@@ -8,27 +8,27 @@ module Lazy ( run, thunk, map, apply, bind
88import Basics ((<|), (.))
99import Native.Lazy
1010
11−data Lazy a = L { run : () -> a }
11+data Lazy a = L { force : () -> a }
1212
1313{-| Memoize a thunk so it is evaluated at most once. -}
14−thunk : (() -> a) -> Lazy a
15−thunk t = L { run = (Native.Lazy.thunk t) }
14+lazy : (() -> a) -> Lazy a
15+lazy t = L { force = (Native.Lazy.lazy t) }
1616
1717{-| Execute a lazy value. -}
18−run : Lazy a -> a
19−run (L r) = r.run ()
18+force : Lazy a -> a
19+force (L r) = r.force ()
2020
2121{-| Lazily apply a pure function to a lazy value. -}
2222map : (a -> b) -> Lazy a -> Lazy b
23−map f t = thunk <| \() ->
24− f . run <| t
23+map f t = lazy <| \() ->
24+ f . force <| t
2525
2626{-| Lazily apply a Lazy function to a Lazy value. -}
2727apply : Lazy (a -> b) -> Lazy a -> Lazy b
28−apply f x = thunk <| \() ->
29− (run f) (run x)
28+apply f x = lazy <| \() ->
29+ (force f) (force x)
3030
3131{-| Lazily chain together Lazy computations. -}
3232bind : Lazy a -> (a -> Lazy b) -> Lazy b
33−bind x k = thunk <| \() ->
34− run . k . run <| x
33+bind x k = lazy <| \() ->
34+ force . k . force <| x
libraries/Lazy/Stream.elm+37−39
@@ -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
libraries/Native/Lazy.js+2−2
@@ -7,7 +7,7 @@ Elm.Native.Lazy.make = function(elm) {
77
88 var Utils = Elm.Native.Utils.make(elm);
99
10− function thunk(f) {
10+ function lazy(f) {
1111 var value = {};
1212 var isEvaled = false;
1313 var memoed = function(x) {
@@ -21,6 +21,6 @@ Elm.Native.Lazy.make = function(elm) {
2121 }
2222
2323 return elm.Native.Lazy.values = {
24− thunk : thunk
24+ lazy : lazy
2525 };
2626};