on master help sign in
Merge branch 'Veladus-master'
commit7c997d87810e4049654cf2d2442e4dbe8db60470 authorHarendra Kumar <harendra.kumar@gmail.com> dateSat 17 Mar 2018, 02:38 (9 years ago) parent29bc66492e8f2b
2 files changed+45−4
src/Streamly/Prelude.hs+32−1 test/Main.hs+13−3
src/Streamly/Prelude.hs+32−1
@@ -39,10 +39,13 @@ module Streamly.Prelude
3939 , all
4040 , any
4141 , head
42+ , tail
4243 , last
44+ , null
4345 , length
4446 , elem
4547 , notElem
48+ , reverse
4649 , maximum
4750 , minimum
4851 , sum
@@ -82,7 +85,7 @@ import Prelude hiding (filter, drop, dropWhile, take,
8285 mapM, mapM_, sequence, all, any,
8386 sum, product, elem, notElem,
8487 maximum, minimum, head, last,
85− length)
88+ tail, length, null, reverse)
8689import qualified Prelude
8790import qualified System.IO as IO
8891
@@ -333,11 +336,26 @@ head m =
333336 yield a _ = return (Just a)
334337 in (runStream (toStream m)) Nothing stop yield
335338
339+-- | Extract all but the first element of the stream, if any.
340+tail :: (Streaming t, Monad m) => t m a -> m (Maybe (t m a))
341+tail m =
342+ let stop = return Nothing
343+ yield _ Nothing = return $ Just nil
344+ yield _ (Just t) = return $ Just $ fromStream t
345+ in (runStream (toStream m)) Nothing stop yield
346+
336347-- | Extract the last element of the stream, if any.
337348{-# INLINE last #-}
338349last :: (Streaming t, Monad m) => t m a -> m (Maybe a)
339350last = foldl (\_ y -> Just y) Nothing id
340351
352+-- | Determine whether the stream is empty.
353+null :: (Streaming t, Monad m) => t m a -> m Bool
354+null m =
355+ let stop = return True
356+ yield _ _ = return False
357+ in (runStream (toStream m)) Nothing stop yield
358+
341359-- | Determine whether an element is present in the stream.
342360elem :: (Streaming t, Monad m, Eq a) => a -> t m a -> m Bool
343361elem e m = go (toStream m)
@@ -362,6 +380,19 @@ notElem e m = go (toStream m)
362380length :: (Streaming t, Monad m) => t m a -> m Int
363381length = foldl (\n _ -> n + 1) 0 id
364382
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 $ \svr stp yld ->
389+ let stop = case rev of
390+ Nothing -> stp
391+ Just str -> runStream str svr stp yld
392+ yield a Nothing = runStream (a `scons` rev) svr stp yld
393+ yield a (Just x) = runStream (go (Just $ a `scons` rev) x) svr stp yld
394+ in runStream rest svr stop yield
395+
365396-- XXX replace the recursive "go" with continuation
366397-- | Determine the minimum element in a stream.
367398minimum :: (Streaming t, Monad m, Ord a) => t m a -> m (Maybe a)
test/Main.hs+13−3
@@ -7,6 +7,7 @@ import Control.Concurrent (threadDelay)
77import Control.Monad (replicateM)
88import Data.Foldable (forM_)
99import Data.List (sort)
10+import Data.Maybe (fromJust)
1011import Test.Hspec
1112
1213import Streamly
@@ -675,7 +676,9 @@ streamOperations (stream, list, len) = do
675676 it "dropWhile < some" $ transform (A.dropWhile (< (len `div` 2)))
676677 (dropWhile (< (len `div` 2)))
677678
679+ -- Transformations
678680 it "scan left" $ transform (A.scan (+) 0 id) (scanl (+) 0)
681+ it "reverse" $ transform A.reverse reverse
679682
680683 -- Elimination
681684 it "foldl" $ elimination (A.foldl (+) 0 id) (foldl (+) 0)
@@ -695,14 +698,21 @@ streamOperations (stream, list, len) = do
695698 it "last empty" $ A.last stream `shouldReturn` Nothing
696699 it "maximum empty" $ A.maximum stream `shouldReturn` Nothing
697700 it "minimum empty" $ A.minimum stream `shouldReturn` Nothing
701+ it "null empty" $ A.null stream `shouldReturn` True
702+ it "tail empty" $ (A.tail stream >>= return . maybe True (const False))
703+ `shouldReturn` True
698704 else do
699705 it "head nonEmpty" $ A.head stream `shouldReturn` Just (head list)
700706 it "last nonEmpty" $ A.last stream `shouldReturn` Just (last list)
701− it "maximum nonEmpty" $ A.maximum stream `shouldReturn` Just (maximum list)
702− it "minimum nonEmpty" $ A.minimum stream `shouldReturn` Just (minimum list)
707+ it "maximum nonEmpty" $ A.maximum stream
708+ `shouldReturn` Just (maximum list)
709+ it "minimum nonEmpty" $ A.minimum stream
710+ `shouldReturn` Just (minimum list)
711+ it "null nonEmpty" $ A.null stream `shouldReturn` False
712+ it "tail nonEmpty" $ (A.tail stream >>= A.toList . fromJust)
713+ `shouldReturn` tail list
703714
704715 where
705−
706716 -- XXX run on empty stream as well
707717 transform streamOp listOp =
708718 (A.toList $ streamOp stream) `shouldReturn` listOp list