on master help sign in
Add some basic functions to Lazy library
d97d542Max New
libraries/Lazy.elm+18−2
@@ -1,18 +1,34 @@
1−module Lazy ( run, thunk
1+module Lazy ( run, thunk, map, apply, bind
2 ) where
2 ) where
3
3
4{-| Library for efficient Lazy evaluation.
4{-| Library for efficient Lazy evaluation.
5
5
6-}
6-}
7
7
8+import Basics ((<|), (.))
8import Native.Lazy
9import Native.Lazy
9
10
10data Lazy a = L { run : () -> a }
11data Lazy a = L { run : () -> a }
11
12
12−{-| Memoize a thunk so it is evaluated at most once -}
13+{-| Memoize a thunk so it is evaluated at most once. -}
13thunk : (() -> a) -> Lazy a
14thunk : (() -> a) -> Lazy a
14thunk t = L { run = (Native.Lazy.thunk t) }
15thunk t = L { run = (Native.Lazy.thunk t) }
15
16
17+{-| Execute a lazy value. -}
16run : Lazy a -> a
18run : Lazy a -> a
17run (L r) = r.run ()
19run (L r) = r.run ()
18
20
21+{-| Lazily apply a pure function to a lazy value. -}
22+map : (a -> b) -> Lazy a -> Lazy b
23+map f t = thunk <| \() ->
24+ f . run <| t
25+
26+{-| Lazily apply a Lazy function to a Lazy value. -}
27+apply : Lazy (a -> b) -> Lazy a -> Lazy b
28+apply f x = thunk <| \() ->
29+ (run f) (run x)
30+
31+{-| Lazily chain together Lazy computations. -}
32+bind : Lazy a -> (a -> Lazy b) -> Lazy b
33+bind x k = thunk <| \() ->
34+ run . k . run <| x