on master help sign in
Merge pull request #379 from maxsnew/lazy

Increase sharing in Stream combinators

commit1fbb635c51a6f31bb82244da7ffe664f5e0dfffe authorEvan Czaplicki <info@elm-lang.org> dateMon 9 Dec 2013, 17:16 (13 years ago) parent92949f916d8753
1 file changed+9−5
libraries/Lazy/Stream.elm+9−5
libraries/Lazy/Stream.elm+9−5
@@ -51,18 +51,22 @@ cons' = S . lazy
5151-}
5252iterate : (a -> a) -> a -> Stream a
5353iterate f x = cons' <| \() ->
54− (x, map f (iterate f x))
54+ (x, iterate f (f x))
5555
5656{-| Create an infinite Stream of xs. -}
5757repeat : a -> Stream a
58−repeat x = iterate id x
58+repeat x = let go = cons x <| \() -> go
59+ in go
5960
6061{-| Cycle through the elements of the nonempty list (x :: xs) -}
6162cycle : a -> [a] -> Stream a
6263cycle x xs = let cycle' ys = case ys of
63− [] -> cons' <| \() ->
64− (x, cycle' xs)
65− in cycle' []
64+ [] -> go
65+ (y :: ys) -> cons y <| \() ->
66+ cycle' ys
67+ go = cons' <| \() ->
68+ (x, cycle' xs)
69+ in go
6670
6771{-| Apply a function to every element of a Stream. -}
6872map : (a -> b) -> Stream a -> Stream b