on master help sign in
Clarify docs for `splitWith` and make note about potential infinite loops more consistent
f57ffdfEvan Czaplicki
libraries/Lazy/Stream.elm+14−8
@@ -217,9 +217,7 @@ filter isOdd naturals
217filter isOdd twos
217filter isOdd twos
218```
218```
219
219
220−The produced Stream can go into an infinite loop if no more elements
221−satisfy the predicate. So be sure that there are infinitely many terms
222−in the stream that satisfy the predicate.
220+This can infinite loop if no more elements in the stream satisfy the predicate!
223-}
221-}
224filter : (a -> Bool) -> Stream a -> Stream a
222filter : (a -> Bool) -> Stream a -> Stream a
225filter pred xs = cons' <| (\() ->
223filter pred xs = cons' <| (\() ->
@@ -237,12 +235,13 @@ takeWhile (\n -> n < 10) powersOf2 == [2,4,8]
237-- Bad! Infinite loop!
235-- Bad! Infinite loop!
238takeWhile isEven powersOf2
236takeWhile isEven powersOf2
239```
237```
238+
239+This can infinite loop if all elements of the stream satisfy the predicate!
240-}
240-}
241takeWhile : (a -> Bool) -> Stream a -> [a]
241takeWhile : (a -> Bool) -> Stream a -> [a]
242takeWhile pred xs = fst <| splitWith pred xs
242takeWhile pred xs = fst <| splitWith pred xs
243
243
244{-| Drop values from the stream as long as the predicate holds.
244{-| Drop values from the stream as long as the predicate holds.
245−This can infinite loop if all elements of the stream satisfy the predicate!
246
245
247```haskell
246```haskell
248-- 16, 32, 64, 128, ...
247-- 16, 32, 64, 128, ...
@@ -251,15 +250,22 @@ dropWhile (\n -> n < 10) powersOf2
251-- Bad! Infinite loop!
250-- Bad! Infinite loop!
252dropWhile isEven powersOf2
251dropWhile isEven powersOf2
253```
252```
253+
254+This can infinite loop if all elements of the stream satisfy the predicate!
254-}
255-}
255dropWhile : (a -> Bool) -> Stream a -> Stream a
256dropWhile : (a -> Bool) -> Stream a -> Stream a
256dropWhile pred xs = snd <| splitWith pred xs
257dropWhile pred xs = snd <| splitWith pred xs
257
258
258−{-| Split a stream when a predicate holds and
259+{-| Combination of `takeWhile` and `dropWhile`. Split a stream when
259−the rest of the Stream. This function only terminates if there is
260+the predicate no longer holds.
260−an element of the stream for which the predicate does not hold.
261+
262+```haskell
263+splitWith pred xs == (takeWhile pred xs, dropWhile pred xs)
264+
265+splitWith (\n -> n < 10) powersOf2 == ([2,4,8], ...)
266+```
261
267
262− splitWith pred xs == (takeWhile pred xs, dropWhile pred xs)
268+This can infinite loop if all elements satisfy the predicate!
263-}
269-}
264splitWith : (a -> Bool) -> Stream a -> ([a], Stream a)
270splitWith : (a -> Bool) -> Stream a -> ([a], Stream a)
265splitWith pred xs = let (hd, tl) = unS xs in
271splitWith pred xs = let (hd, tl) = unS xs in