on master help sign in
Lazy/Stream API changes
46f3fa1Max New
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
2 ) where
2 ) where
3
3
4{-| Library for efficient Lazy evaluation.
4{-| Library for efficient Lazy evaluation.
@@ -8,27 +8,27 @@ module Lazy ( run, thunk, map, apply, bind
8import Basics ((<|), (.))
8import Basics ((<|), (.))
9import Native.Lazy
9import Native.Lazy
10
10
11−data Lazy a = L { run : () -> a }
11+data Lazy a = L { force : () -> a }
12
12
13{-| Memoize a thunk so it is evaluated at most once. -}
13{-| Memoize a thunk so it is evaluated at most once. -}
14−thunk : (() -> a) -> Lazy a
14+lazy : (() -> a) -> Lazy a
15−thunk t = L { run = (Native.Lazy.thunk t) }
15+lazy t = L { force = (Native.Lazy.lazy t) }
16
16
17{-| Execute a lazy value. -}
17{-| Execute a lazy value. -}
18−run : Lazy a -> a
18+force : Lazy a -> a
19−run (L r) = r.run ()
19+force (L r) = r.force ()
20
20
21{-| Lazily apply a pure function to a lazy value. -}
21{-| Lazily apply a pure function to a lazy value. -}
22map : (a -> b) -> Lazy a -> Lazy b
22map : (a -> b) -> Lazy a -> Lazy b
23−map f t = thunk <| \() ->
23+map f t = lazy <| \() ->
24− f . run <| t
24+ f . force <| t
25
25
26{-| Lazily apply a Lazy function to a Lazy value. -}
26{-| Lazily apply a Lazy function to a Lazy value. -}
27apply : Lazy (a -> b) -> Lazy a -> Lazy b
27apply : Lazy (a -> b) -> Lazy a -> Lazy b
28−apply f x = thunk <| \() ->
28+apply f x = lazy <| \() ->
29− (run f) (run x)
29+ (force f) (force x)
30
30
31{-| Lazily chain together Lazy computations. -}
31{-| Lazily chain together Lazy computations. -}
32bind : Lazy a -> (a -> Lazy b) -> Lazy b
32bind : Lazy a -> (a -> Lazy b) -> Lazy b
33−bind x k = thunk <| \() ->
33+bind x k = lazy <| \() ->
34− run . k . run <| x
34+ force . k . force <| x
libraries/Lazy/Stream.elm+37−39
@@ -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
6 ) where
6 ) where
7
7
8{-| Library for Infinite Streams
8{-| Library for Infinite Streams
9
9
10# Observe
10# Observe
11−@docs hd, tl, observeWhen, take, drop, splitAt
11+@docs head, tail, sampleOn, take, drop, splitAt
12
12
13# Create
13# Create
14−@docs mkStream, iterate, repeat, cycle
14+@docs cons', iterate, repeat, cycle
15
15
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)
24
24
25−data Stream a = S (Lazy { hd : a, tl : Stream a })
25+data Stream a = S (Lazy (a, Stream a))
26
26
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
29
29
30−tl : Stream a -> Stream a
30+head : Stream a -> a
31−tl (S t) = (run t).tl
31+head = fst . unS
32
32
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+
33−mkStream : (() -> { hd : a, tl : Stream a }) -> Stream a
41+cons' : (() -> (a, Stream a)) -> Stream a
34−mkStream = S . thunk
42+cons' = S . lazy
35
43
36iterate : (a -> a) -> a -> Stream a
44iterate : (a -> a) -> a -> Stream a
37−iterate f x = mkStream <| \() ->
45+iterate f x = cons' <| \() ->
38− { hd = x
39− , tl = map f (iterate f x)
46+ (x, map f (iterate f x))
40− }
41
47
42repeat : a -> Stream a
48repeat : a -> Stream a
43repeat x = iterate id x
49repeat x = iterate id x
44
50
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' <| \() ->
48− { hd = x
49− , tl = cycle' xs }
54+ (x, cycle' xs)
50− (y :: ys) -> mkStream <| \() ->
51− { hd = y
52− , tl = cycle' ys }
53 in cycle' []
55 in cycle' []
54
56
55map : (a -> b) -> Stream a -> Stream b
57map : (a -> b) -> Stream a -> Stream b
56−map f xs = mkStream <| \() ->
58+map f xs = cons' <| \() ->
57− { hd = f (hd xs)
58− , tl = map f (tl xs)
59+ (f (head xs), map f (tail xs))
59− }
60
60
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))
66
66
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))
71− }
72
71
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))
77− }
78
76
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)
90
88
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
libraries/Native/Lazy.js+2−2
@@ -7,7 +7,7 @@ Elm.Native.Lazy.make = function(elm) {
7
7
8 var Utils = Elm.Native.Utils.make(elm);
8 var Utils = Elm.Native.Utils.make(elm);
9
9
10− function thunk(f) {
10+ function lazy(f) {
11 var value = {};
11 var value = {};
12 var isEvaled = false;
12 var isEvaled = false;
13 var memoed = function(x) {
13 var memoed = function(x) {
@@ -21,6 +21,6 @@ Elm.Native.Lazy.make = function(elm) {
21 }
21 }
22
22
23 return elm.Native.Lazy.values = {
23 return elm.Native.Lazy.values = {
24− thunk : thunk
24+ lazy : lazy
25 };
25 };
26};
26};