on master help sign in
Added Prelude.reverse
0586029Veladus
src/Streamly/Prelude.hs+17−2
@@ -45,6 +45,7 @@ module Streamly.Prelude
45 , length
45 , length
46 , elem
46 , elem
47 , notElem
47 , notElem
48+ , reverse
48 , maximum
49 , maximum
49 , minimum
50 , minimum
50 , sum
51 , sum
@@ -84,7 +85,7 @@ import Prelude hiding (filter, drop, dropWhile, take,
84 mapM, mapM_, sequence, all, any,
85 mapM, mapM_, sequence, all, any,
85 sum, product, elem, notElem,
86 sum, product, elem, notElem,
86 maximum, minimum, head, last,
87 maximum, minimum, head, last,
87− tail, length, null)
88+ tail, length, null, reverse)
88import qualified Prelude
89import qualified Prelude
89import qualified System.IO as IO
90import qualified System.IO as IO
90
91
@@ -348,7 +349,7 @@ tail m =
348last :: (Streaming t, Monad m) => t m a -> m (Maybe a)
349last :: (Streaming t, Monad m) => t m a -> m (Maybe a)
349last = foldl (\_ y -> Just y) Nothing id
350last = foldl (\_ y -> Just y) Nothing id
350
351
351−-- | Determine wheter the stream is empty
352+-- | Determine whether the stream is empty.
352null :: (Streaming t, Monad m) => t m a -> m Bool
353null :: (Streaming t, Monad m) => t m a -> m Bool
353null m =
354null m =
354 let stop = return True
355 let stop = return True
@@ -379,6 +380,20 @@ notElem e m = go (toStream m)
379length :: (Streaming t, Monad m) => t m a -> m Int
380length :: (Streaming t, Monad m) => t m a -> m Int
380length = foldl (\n _ -> n + 1) 0 id
381length = foldl (\n _ -> n + 1) 0 id
381
382
383+-- | Returns the elements of the stream in reverse order.
384+-- The stream must be finite.
385+reverse :: (Streaming t) => t m a -> t m a
386+reverse m = fromStream $ go Nothing (toStream m)
387+ where
388+ go rev rest = Stream $ \s stp yld ->
389+ let
390+ stop = case rev of
391+ Nothing -> runStream snil s stp yld
392+ Just str -> runStream str s stp yld
393+ yield a Nothing = runStream (a `scons` rev) s stp yld
394+ yield a (Just x) = runStream (go (Just $ a `scons` rev) x) s stp yld
395+ in runStream rest Nothing stop yield
396+
382-- XXX replace the recursive "go" with continuation
397-- XXX replace the recursive "go" with continuation
383-- | Determine the minimum element in a stream.
398-- | Determine the minimum element in a stream.
384minimum :: (Streaming t, Monad m, Ord a) => t m a -> m (Maybe a)
399minimum :: (Streaming t, Monad m, Ord a) => t m a -> m (Maybe a)