@@ -67,6 +67,7 @@ module Streamly.Streams.StreamD
67 -- ** General Folds
67 -- ** General Folds
@@ -82,6 +83,10 @@ module Streamly.Streams.StreamD
86 -- ** Map and Fold
91 -- ** Map and Fold
@@ -110,6 +115,10 @@ module Streamly.Streams.StreamD
110 -- * Mapping
115 -- * Mapping
114 -- ** Map and Filter
123 -- ** Map and Filter
115 , mapMaybe
124 , mapMaybe
@@ -122,6 +131,12 @@ module Streamly.Streams.StreamD
122 -- * Comparisions
131 -- * Comparisions
138+ -- * Transformation comprehensions
@@ -130,7 +145,8 @@ import GHC.Types ( SPEC(..) )
130import Prelude
145import Prelude
131 hiding (map, mapM, mapM_, repeat, foldr, last, take, filter,
146 hiding (map, mapM, mapM_, repeat, foldr, last, take, filter,
132 takeWhile, drop, dropWhile, all, any, maximum, minimum, elem,
147 takeWhile, drop, dropWhile, all, any, maximum, minimum, elem,
133− notElem, null, head, tail, zipWith)
148+ notElem, null, head, tail, zipWith, lookup, foldr1, sequence,
135import Streamly.SVar (MonadAsync, State(..), defState, rstState)
151import Streamly.SVar (MonadAsync, State(..), defState, rstState)
@@ -306,6 +322,14 @@ foldrM f z (Stream step state) = go SPEC state
306foldr :: Monad m => (a -> b -> b) -> b -> Stream m a -> m b
322foldr :: Monad m => (a -> b -> b) -> b -> Stream m a -> m b
307foldr f = foldrM (\a b -> return (f a b))
323foldr f = foldrM (\a b -> return (f a b))
325+{-# INLINE_NORMAL foldr1 #-}
326+foldr1 :: Monad m => (a -> a -> a) -> Stream m a -> m (Maybe a)
330+ Nothing -> return Nothing
331+ Just (h, t) -> fmap Just (foldr f h t)
309{-# INLINE_NORMAL foldlM' #-}
333{-# INLINE_NORMAL foldlM' #-}
310foldlM' :: Monad m => (b -> a -> m b) -> b -> Stream m a -> m b
334foldlM' :: Monad m => (b -> a -> m b) -> b -> Stream m a -> m b
311foldlM' fstep begin (Stream step state) = go SPEC begin state
335foldlM' fstep begin (Stream step state) = go SPEC begin state
@@ -460,6 +484,53 @@ minimum (Stream step state) = go Nothing state
460 Skip s -> go (Just acc) s
484 Skip s -> go (Just acc) s
461 Stop -> return (Just acc)
485 Stop -> return (Just acc)
487+{-# INLINE_NORMAL (!!) #-}
488+(!!) :: (Monad m) => Stream m a -> Int -> m (Maybe a)
489+(Stream step state) !! i = go i state
492+ r <- step defState st
494+ Yield x s | n < 0 -> return Nothing
495+ | n == 0 -> return $ Just x
496+ | otherwise -> go (n - 1) s
498+ Stop -> return Nothing
500+{-# INLINE_NORMAL lookup #-}
501+lookup :: (Monad m, Eq a) => a -> Stream m (a, b) -> m (Maybe b)
502+lookup e (Stream step state) = go state
505+ r <- step defState st
507+ Yield (a, b) s -> if e == a then return (Just b) else go s
509+ Stop -> return Nothing
511+{-# INLINE_NORMAL find #-}
512+find :: Monad m => (a -> Bool) -> Stream m a -> m (Maybe a)
513+find p (Stream step state) = go state
516+ r <- step defState st
518+ Yield x s -> if p x then return (Just x) else go s
520+ Stop -> return Nothing
522+{-# INLINE_NORMAL findIndices #-}
523+findIndices :: Monad m => (a -> Bool) -> Stream m a -> Stream m Int
524+findIndices p (Stream step state) = Stream step' (state, 0)
526+ {-# INLINE_LATE step' #-}
527+ step' gst (st, i) = do
528+ r <- step (rstState gst) st
530+ Yield x s -> if p x then Yield i (s, i+1) else Skip (s, i+1)
531+ Skip s -> Skip (s, i+1)
463------------------------------------------------------------------------------
534------------------------------------------------------------------------------
464-- Map and Fold
535-- Map and Fold
465------------------------------------------------------------------------------
536------------------------------------------------------------------------------
@@ -660,6 +731,41 @@ mapM f (Stream step state) = Stream step' state
660map :: Monad m => (a -> b) -> Stream m a -> Stream m b
731map :: Monad m => (a -> b) -> Stream m a -> Stream m b
661map f = mapM (return . f)
732map f = mapM (return . f)
734+{-# INLINE_NORMAL sequence #-}
735+sequence :: Monad m => Stream m (m a) -> Stream m a
736+sequence (Stream step state) = Stream step' state
738+ {-# INLINE_LATE step' #-}
740+ r <- step (rstState gst) st
742+ Yield x s -> x >>= \a -> return (Yield a s)
743+ Skip s -> return $ Skip s
746+------------------------------------------------------------------------------
748+------------------------------------------------------------------------------
750+{-# INLINE_NORMAL insertBy #-}
751+insertBy :: Monad m => (a -> a -> Ordering) -> a -> Stream m a -> Stream m a
752+insertBy cmp a (Stream step state) = Stream step' (state, False)
754+ step' gst (st, False) = do
755+ r <- step (rstState gst) st
757+ Yield x s -> case cmp a x of
758+ GT -> return $ Yield x (s, False)
759+ _ -> return $ Yield a (st, True)
760+ Skip s -> return $ Skip (s, False)
761+ Stop -> return $ Yield a (st, True)
762+ step' gst (st, True) = do
763+ r <- step (rstState gst) st
765+ Yield x s -> return $ Yield x (s, True)
766+ Skip s -> return $ Skip (s, True)
663------------------------------------------------------------------------------
769------------------------------------------------------------------------------
664-- Transformation by Map and Filter
770-- Transformation by Map and Filter
665------------------------------------------------------------------------------
771------------------------------------------------------------------------------
@@ -674,7 +780,7 @@ mapMaybeM :: Monad m => (a -> m (Maybe b)) -> Stream m a -> Stream m b
674mapMaybeM f = fmap fromJust . filter isJust . mapM f
780mapMaybeM f = fmap fromJust . filter isJust . mapM f
676------------------------------------------------------------------------------
782------------------------------------------------------------------------------
677−-- Instances
783+-- Zipping
678------------------------------------------------------------------------------
784------------------------------------------------------------------------------
680{-# INLINE_NORMAL zipWithM #-}
786{-# INLINE_NORMAL zipWithM #-}
@@ -768,6 +874,92 @@ cmpBy cmp (Stream step1 t1) (Stream step2 t2) = cmp_loop0 SPEC t1 t2
768 Skip s2' -> cmp_null s2'
874 Skip s2' -> cmp_null s2'
769 Stop -> return EQ
875 Stop -> return EQ
877+------------------------------------------------------------------------------
879+------------------------------------------------------------------------------
881+data MergeWithState l r o
883+ | MergeWithLeft l r o
884+ | MergeWithRight l r o
885+ | MergeNoMore (Either l r)
887+{-# INLINE_NORMAL mergeBy #-}
888+mergeBy :: (Monad m) => (a -> a -> Ordering) -> Stream m a -> Stream m a -> Stream m a
889+mergeBy f (Stream stepa ta) (Stream stepb tb) =
890+ Stream step (MergeBoth ta tb)
891+ {-# INLINE_LATE step #-}
892+ step gst (MergeBoth sa sb) = do
893+ ra <- stepa (rstState gst) sa
894+ rb <- stepb (rstState gst) sb
896+ (Yield xa sa', Yield xb sb') -> do
899+ return $ Yield xa (MergeWithRight sa' sb' xb)
901+ return $ Yield xb (MergeWithLeft sa' sb' xa)
902+ (Yield xa sa', Stop) -> return $ Yield xa (MergeNoMore (Left sa'))
903+ (Stop, Yield xb sb') -> return $ Yield xb (MergeNoMore (Right sb'))
904+ (Yield xa sa', Skip sb') -> return $ Skip (MergeWithLeft sa' sb' xa)
905+ (Skip sa', Yield xb sb') -> return $ Skip (MergeWithRight sa' sb' xb)
906+ (Skip sa', Skip sb') -> return $ Skip (MergeBoth sa' sb')
907+ (Skip sa', Stop) -> return $ Skip (MergeNoMore (Left sa'))
908+ (Stop, Skip sb') -> return $ Skip (MergeNoMore (Right sb'))
909+ (Stop, Stop) -> return Stop
910+ step gst (MergeWithLeft sa sb x) = do
911+ rb <- stepb (rstState gst) sb
915+ LT -> return $ Yield x (MergeWithRight sa sb' xb)
916+ _ -> return $ Yield xb (MergeWithLeft sa sb' x)
917+ Skip sb' -> return $ Skip (MergeWithLeft sa sb' x)
918+ Stop -> return $ Yield x (MergeNoMore (Left sa))
919+ step gst (MergeWithRight sa sb x) = do
920+ ra <- stepa (rstState gst) sa
924+ LT -> return $ Yield xa (MergeWithRight sa' sb x)
925+ _ -> return $ Yield x (MergeWithLeft sa' sb xa)
926+ Skip sa' -> return $ Skip (MergeWithRight sa' sb x)
927+ Stop -> return $ Yield x (MergeNoMore (Right sb))
928+ step gst (MergeNoMore (Left sa)) = do
929+ ra <- stepa (rstState gst) sa
931+ Yield xa sa' -> return $ Yield xa (MergeNoMore (Left sa'))
932+ Skip sa' -> return $ Skip (MergeNoMore (Left sa'))
934+ step gst (MergeNoMore (Right sb)) = do
935+ rb <- stepb (rstState gst) sb
937+ Yield xb sb' -> return $ Yield xb (MergeNoMore (Right sb'))
938+ Skip sb' -> return $ Skip (MergeNoMore (Right sb'))
941+------------------------------------------------------------------------------
942+-- Transformation comprehensions
943+------------------------------------------------------------------------------
945+{-# INLINE_NORMAL the #-}
946+the :: (Eq a, Monad m) => Stream m a -> m (Maybe a)
947+the (Stream step state) = go state
950+ r <- step defState st
952+ Yield x s -> go' x s
954+ Stop -> return Nothing
956+ r <- step defState st
958+ Yield x s | x == n -> go' n s
959+ | otherwise -> return Nothing
961+ Stop -> return (Just n)
771------------------------------------------------------------------------------
963------------------------------------------------------------------------------
772-- Instances
964-- Instances
773------------------------------------------------------------------------------
965------------------------------------------------------------------------------