on master help sign in
Add some basic functions to Lazy library
commitd97d54213cad8283a760381b54445a1c020074e1 authorMax New <maxsnew@gmail.com> dateMon 9 Dec 2013, 00:39 (13 years ago) parent9f98ec9
1 file changed+18−2
libraries/Lazy.elm+18−2
libraries/Lazy.elm+18−2
@@ -1,18 +1,34 @@
1−module Lazy ( run, thunk
1+module Lazy ( run, thunk, map, apply, bind
22 ) where
33
44{-| Library for efficient Lazy evaluation.
55
66-}
77
8+import Basics ((<|), (.))
89import Native.Lazy
910
1011data Lazy a = L { run : () -> a }
1112
12−{-| Memoize a thunk so it is evaluated at most once -}
13+{-| Memoize a thunk so it is evaluated at most once. -}
1314thunk : (() -> a) -> Lazy a
1415thunk t = L { run = (Native.Lazy.thunk t) }
1516
17+{-| Execute a lazy value. -}
1618run : Lazy a -> a
1719run (L r) = r.run ()
1820
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