@@ -45,6 +45,7 @@ module Streamly.Prelude
4545 , length
4646 , elem
4747 , notElem
48+ , reverse
4849 , maximum
4950 , minimum
5051 , sum
@@ -84,7 +85,7 @@ import Prelude hiding (filter, drop, dropWhile, take,
8485 mapM, mapM_, sequence, all, any,
8586 sum, product, elem, notElem,
8687 maximum, minimum, head, last,
87− tail, length, null)
88+ tail, length, null, reverse)
8889import qualified Prelude
8990import qualified System.IO as IO
9091
@@ -348,7 +349,7 @@ tail m =
348349last :: (Streaming t, Monad m) => t m a -> m (Maybe a)
349350last = foldl (\_ y -> Just y) Nothing id
350351
351−-- | Determine wheter the stream is empty
352+-- | Determine whether the stream is empty.
352353null :: (Streaming t, Monad m) => t m a -> m Bool
353354null m =
354355 let stop = return True
@@ -379,6 +380,20 @@ notElem e m = go (toStream m)
379380length :: (Streaming t, Monad m) => t m a -> m Int
380381length = foldl (\n _ -> n + 1) 0 id
381382
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+
382397-- XXX replace the recursive "go" with continuation
383398-- | Determine the minimum element in a stream.
384399minimum :: (Streaming t, Monad m, Ord a) => t m a -> m (Maybe a)