2-- Module : Streamly.Internal.Data.Stream.Transform
3-- Copyright : (c) 2017 Composewell Technologies
4-- License : BSD-3-Clause
5-- Maintainer : streamly@composewell.com
6-- Stability : experimental
9module Streamly.Internal.Data.Stream.Transform
12 -- | Pass through a 'Pipe'.
20 -- | Stateless one-to-one maps.
24 -- * Mapping Side Effects (Observation)
25 -- | See also the intersperse*_ combinators.
37 -- | Produce a subset of the stream using criteria based on the values of
38 -- the elements. We can use a concatMap and scan for filtering but these
39 -- combinators are more efficient and convenient.
53 -- | Produce a subset of the stream trimmed at ends.
62 -- * Inserting Elements
63 -- | Produce a superset of the stream. This is the opposite of
64 -- filtering/sampling. We can always use concatMap and scan for inserting
65 -- but these combinators are more efficient and convenient.
67 -- Element agnostic (Opposite of sampling)
69 , intersperseM -- XXX naming
73 , intersperseSuffixBySpan
75 -- , interspersePrefix
76 -- , interspersePrefixBySpan
78 -- * Inserting Side Effects/Time
79 , intersperseM_ -- XXX naming
86 -- * Element Aware Insertion
87 -- | Opposite of filtering
89 -- , intersperseByBefore
90 -- , intersperseByAfter
97 -- * Position Indexing
102 , findIndices -- XXX indicesBy
103 , elemIndices -- XXX indicesOf
106 -- | Map using the previous element.
112 -- Move these to Streamly.Data.Maybe.Stream?
113 , catMaybes -- XXX justs (like lefts/rights)
118 -- Move these to Streamly.Data.Either.Stream?
127import Control.Concurrent (threadDelay)
128import Control.Monad (void)
129import Control.Monad.IO.Class (MonadIO (liftIO))
130import Control.Monad.Trans.Class (MonadTrans(..))
131import Data.Either (fromLeft, isLeft, isRight, fromRight)
132import Data.Maybe (isJust, fromJust)
134import Streamly.Internal.Data.Fold.Type (Fold)
135import Streamly.Internal.Data.Pipe (Pipe)
137import qualified Streamly.Internal.Data.Fold as FL
138import qualified Streamly.Internal.Data.Stream.StreamD.Transform as D
139import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
140import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
142import Streamly.Internal.Data.Stream.Bottom
143import Streamly.Internal.Data.Stream.Type
146 ( filter, drop, dropWhile, take, takeWhile, foldr, map, mapM, sequence
147 , reverse, foldr1 , repeat, scanl, scanl1, zipWith)
152-- >>> import Control.Concurrent (threadDelay)
153-- >>> import Control.Monad.Trans (lift)
154-- >>> import Control.Monad.Trans.Identity (runIdentityT)
155-- >>> import Data.Function ((&))
156-- >>> import Data.Maybe (fromJust, isJust)
157-- >>> import Prelude hiding (filter, drop, dropWhile, take, takeWhile, foldr, map, mapM, sequence, reverse, foldr1 , scanl, scanl1)
158-- >>> import Streamly.Internal.Data.Stream as Stream
159-- >>> import qualified Streamly.Data.Fold as Fold
160-- >>> import qualified Streamly.Internal.Data.Fold as Fold
161-- >>> import qualified Streamly.Internal.Data.Unfold as Unfold
162-- >>> import System.IO (stdout, hSetBuffering, BufferMode(LineBuffering))
164-- >>> hSetBuffering stdout LineBuffering
166-- XXX because of the use of D.cons for appending, folds and scans have
167-- quadratic complexity when iterated over a stream. We should use StreamK for
168-- linear performance on iteration.
170------------------------------------------------------------------------------
172------------------------------------------------------------------------------
174-- | Use a 'Pipe' to transform a stream.
178{-# INLINE transform #-}
179transform :: Monad m => Pipe m a b -> Stream m a -> Stream m b
180transform pipe xs = fromStreamD $ D.transform pipe (toStreamD xs)
182------------------------------------------------------------------------------
183-- Transformation Folds
184------------------------------------------------------------------------------
186-- | Right fold to a streaming monad.
188-- > foldrS Stream.cons Stream.nil === id
190-- 'foldrS' can be used to perform stateless stream to stream transformations
191-- like map and filter in general. It can be coupled with a scan to perform
192-- stateful transformations. However, note that the custom map and filter
193-- routines can be much more efficient than this due to better stream fusion.
195-- >>> input = Stream.unfold Unfold.fromList [1..5]
196-- >>> Stream.fold Fold.toList $ Stream.foldrS Stream.cons Stream.nil input
199-- Find if any element in the stream is 'True':
201-- >>> step x xs = if odd x then return True else xs
202-- >>> input = Stream.unfold Unfold.fromList (2:4:5:undefined) :: Stream IO Int
203-- >>> Stream.fold Fold.toList $ Stream.foldrS step (return False) input
206-- Map (+2) on odd elements and filter out the even elements:
208-- >>> step x xs = if odd x then (x + 2) `Stream.cons` xs else xs
209-- >>> input = Stream.unfold Unfold.fromList [1..5] :: Stream IO Int
210-- >>> Stream.fold Fold.toList $ Stream.foldrS step Stream.nil input
216 (a -> Stream m b -> Stream m b)
223 (\y ys -> toStreamK $ f y (fromStreamK ys))
227-- | Right fold to a transformer monad. This is the most general right fold
228-- function. 'foldrS' is a special case of 'foldrT', however 'foldrS'
229-- implementation can be more efficient:
231-- >>> foldrS = foldrT
233-- >>> step f x xs = lift $ f x (runIdentityT xs)
234-- >>> foldrM f z s = runIdentityT $ foldrT (step f) (lift z) s
236-- 'foldrT' can be used to translate streamly streams to other transformer
237-- monads e.g. to a different streaming type.
241foldrT :: (Monad m, Monad (s m), MonadTrans s)
242 => (a -> s m b -> s m b) -> s m b -> Stream m a -> s m b
243foldrT f z s = D.foldrT f z (toStreamD s)
245------------------------------------------------------------------------------
246-- Transformation by Mapping
247------------------------------------------------------------------------------
250-- >>> mapM f = Stream.sequence . fmap f
252-- Apply a monadic function to each element of the stream and replace it with
253-- the output of the resulting action.
255-- >>> s = Stream.unfold Unfold.fromList ["a", "b", "c"]
256-- >>> Stream.fold Fold.drain $ Stream.mapM putStr s
262mapM :: Monad m => (a -> m b) -> Stream m a -> Stream m b
263mapM f m = fromStreamK $ D.toStreamK $ D.mapM f $ toStreamD m
266-- sequence = Stream.mapM id
268-- Replace the elements of a stream of monadic actions with the outputs of
271-- >>> s = Stream.unfold Unfold.fromList [putStr "a", putStr "b", putStrLn "c"]
272-- >>> Stream.fold Fold.drain $ Stream.sequence s
275{-# INLINE sequence #-}
276sequence :: Monad m => Stream m (m a) -> Stream m a
279------------------------------------------------------------------------------
280-- Mapping side effects
281------------------------------------------------------------------------------
283-- | Tap the data flowing through a stream into a 'Fold'. For example, you may
284-- add a tap to log the contents flowing through the stream. The fold is used
285-- only for effects, its result is discarded.
290-- -----stream m a ---------------stream m a-----
294-- >>> s = Stream.unfold Unfold.enumerateFromTo (1, 2)
295-- >>> Stream.fold Fold.drain $ Stream.tap (Fold.drainBy print) s
299-- Compare with 'trace'.
302tap :: Monad m => FL.Fold m a b -> Stream m a -> Stream m a
303tap f xs = fromStreamD $ D.tap f (toStreamD xs)
305-- | Apply a monadic function to each element flowing through the stream and
306-- discard the results.
308-- >>> s = Stream.unfold Unfold.enumerateFromTo (1, 2)
309-- >>> Stream.fold Fold.drain $ Stream.trace print s
313-- Compare with 'tap'.
316trace :: Monad m => (a -> m b) -> Stream m a -> Stream m a
317trace f = mapM (\x -> void (f x) >> return x)
319-- | Perform a side effect before yielding each element of the stream and
320-- discard the results.
322-- >>> s = Stream.unfold Unfold.enumerateFromTo (1, 2)
323-- >>> Stream.fold Fold.drain $ Stream.trace_ (print "got here") s
327-- Same as 'interspersePrefix_' but always serial.
333trace_ :: Monad m => m b -> Stream m a -> Stream m a
334trace_ eff = fromStreamD . D.mapM (\x -> eff >> return x) . toStreamD
336------------------------------------------------------------------------------
337-- Scanning with a Fold
338------------------------------------------------------------------------------
340-- XXX It may be useful to have a version of scan where we can keep the
341-- accumulator independent of the value emitted. So that we do not necessarily
342-- have to keep a value in the accumulator which we are not using. We can pass
343-- an extraction function that will take the accumulator and the current value
344-- of the element and emit the next value in the stream. That will also make it
345-- possible to modify the accumulator after using it. In fact, the step function
346-- can return new accumulator and the value to be emitted. The signature would
347-- be more like mapAccumL.
349-- | Strict left scan. Scan a stream using the given monadic fold.
351-- >>> s = Stream.unfold Unfold.fromList [1..10]
352-- >>> Stream.fold Fold.toList $ Stream.takeWhile (< 10) $ Stream.scan Fold.sum s
355-- >>> scanl' step z = Stream.scan (Fold.foldl' step z)
357-- Like 'map', 'scanl'' too is a one to one transformation,
358-- however it adds an extra element.
360-- >>> s = Stream.unfold Unfold.fromList [1,2,3,4]
361-- >>> Stream.fold Fold.toList $ scanl' (+) 0 s
364-- >>> Stream.fold Fold.toList $ scanl' (flip (:)) [] s
365-- [[],[1],[2,1],[3,2,1],[4,3,2,1]]
367-- The output of 'scanl'' is the initial value of the accumulator followed by
368-- all the intermediate steps and the final result of 'foldl''.
370-- By streaming the accumulated state after each fold step, we can share the
371-- state across multiple stages of stream composition. Each stage can modify or
372-- extend the state, do some processing with it and emit it for the next stage,
373-- thus modularizing the stream processing. This can be useful in
374-- stateful or event-driven programming.
376-- Consider the following monolithic example, computing the sum and the product
377-- of the elements in a stream in one go using a @foldl'@:
379-- >>> foldl' step z = Stream.fold (Fold.foldl' step z)
380-- >>> foldl' (\(s, p) x -> (s + x, p * x)) (0,1) s
383-- Using @scanl'@ we can make it modular by computing the sum in the first
384-- stage and passing it down to the next stage for computing the product:
387-- foldl' (\(_, p) (s, x) -> (s, p * x)) (0,1)
388-- $ scanl' (\(s, _) x -> (s + x, x)) (0,1)
389-- $ Stream.unfold Unfold.fromList [1,2,3,4]
393-- IMPORTANT: 'scanl'' evaluates the accumulator to WHNF. To avoid building
394-- lazy expressions inside the accumulator, it is recommended that a strict
395-- data structure is used for accumulator.
397-- See also: 'usingStateT'
400scan :: Monad m => Fold m a b -> Stream m a -> Stream m b
401scan fld m = fromStreamD $ D.scanOnce fld $ toStreamD m
403-- | Like 'scan' but restarts scanning afresh when the scanning fold
407{-# INLINE scanMany #-}
408scanMany :: Monad m => Fold m a b -> Stream m a -> Stream m b
409scanMany fld m = fromStreamD $ D.scanMany fld $ toStreamD m
411------------------------------------------------------------------------------
413------------------------------------------------------------------------------
415-- | Modify a @Stream m a -> Stream m a@ stream transformation that accepts a
416-- predicate @(a -> b)@ to accept @((s, a) -> b)@ instead, provided a
417-- transformation @Stream m a -> Stream m (s, a)@. Convenient to filter with
420-- >>> filterWithIndex = Stream.with Stream.indexed Stream.filter
425 (Stream m a -> Stream m (s, a))
426 -> (((s, a) -> b) -> Stream m (s, a) -> Stream m (s, a))
427 -> (((s, a) -> b) -> Stream m a -> Stream m a)
428with f comb g = fmap snd . comb g . f
430-- | Include only those elements that pass a predicate.
433filter :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
434filter p m = fromStreamD $ D.filter p $ toStreamD m
436-- | Same as 'filter' but with a monadic predicate.
438{-# INLINE filterM #-}
439filterM :: Monad m => (a -> m Bool) -> Stream m a -> Stream m a
440filterM p m = fromStreamD $ D.filterM p $ toStreamD m
442-- | Use a filtering fold on a stream.
444-- >>> input = Stream.unfold Unfold.fromList [1..10]
445-- >>> Stream.fold Fold.sum $ Stream.foldFilter (Fold.satisfy (> 5)) input
450{-# INLINE foldFilter #-}
451foldFilter :: Monad m => Fold m a (Maybe b) -> Stream m a -> Stream m b
452foldFilter p = fromStreamD . D.foldFilter p . toStreamD
454-- | Drop repeated elements that are adjacent to each other using the supplied
455-- comparison function.
457-- >>> uniq = Stream.uniqBy (==)
459-- To strip duplicate path separators:
461-- >>> input = Stream.unfold Unfold.fromList "//a//b"
462-- >>> f x y = x == '/' && y == '/'
463-- >>> Stream.fold Fold.toList $ Stream.uniqBy f input
474 (a -> a -> Bool) -> Stream m a -> Stream m a
475uniqBy eq = catMaybes . rollingMap f
482 Just x -> if x `eq` curr then Nothing else Just curr
484-- | Drop repeated elements that are adjacent to each other.
487uniq :: (Eq a, Monad m) => Stream m a -> Stream m a
488uniq = fromStreamD . D.uniq . toStreamD
490-- | Strip all leading and trailing occurrences of an element passing a
491-- predicate and make all other consecutive occurrences uniq.
493-- >> prune p = Stream.dropWhileAround p $ Stream.uniqBy (x y -> p x && p y)
496-- > Stream.prune isSpace (Stream.fromList " hello world! ")
506 -- (Monad m, Eq a) =>
507 (a -> Bool) -> Stream m a -> Stream m a
508prune = error "Not implemented yet!"
510-- Possible implementation:
512-- Stream.catMaybes . Stream.parseMany (Parser.groupBy (==) Fold.repeated)@
514-- 'Fold.repeated' should return 'Just' when repeated, and 'Nothing' for a
517-- | Emit only repeated elements, once.
520repeated :: -- (Monad m, Eq a) =>
521 Stream m a -> Stream m a
524-- We can have more efficient implementations for nubOrd and nubInt by using
525-- Set and IntSet to find/remove duplication. For Hashable we can use a
526-- hashmap. Use rewrite rules to specialize to more efficient impls.
528-- | Drop repeated elements anywhere in the stream.
530-- /Caution: not scalable for infinite streams/
535nubBy :: -- Monad m =>
536 (a -> a -> Bool) -> Stream m a -> Stream m a
537nubBy = undefined -- fromStreamD . D.nubBy . toStreamD
539-- | Deletes the first occurrence of the element in the stream that satisfies
540-- the given equality predicate.
542-- >>> input = Stream.unfold Unfold.fromList [1,3,3,5]
543-- >>> Stream.fold Fold.toList $ Stream.deleteBy (==) 3 input
546{-# INLINE deleteBy #-}
547deleteBy :: Monad m => (a -> a -> Bool) -> a -> Stream m a -> Stream m a
548deleteBy cmp x m = fromStreamD $ D.deleteBy cmp x (toStreamD m)
550------------------------------------------------------------------------------
552------------------------------------------------------------------------------
554-- | Same as 'takeWhile' but with a monadic predicate.
556{-# INLINE takeWhileM #-}
557takeWhileM :: Monad m => (a -> m Bool) -> Stream m a -> Stream m a
558takeWhileM p m = fromStreamD $ D.takeWhileM p $ toStreamD m
560-- | Drop elements in the stream as long as the predicate succeeds and then
561-- take the rest of the stream.
563{-# INLINE dropWhile #-}
564dropWhile :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
565dropWhile p m = fromStreamD $ D.dropWhile p $ toStreamD m
567-- | Same as 'dropWhile' but with a monadic predicate.
569{-# INLINE dropWhileM #-}
570dropWhileM :: Monad m => (a -> m Bool) -> Stream m a -> Stream m a
571dropWhileM p m = fromStreamD $ D.dropWhileM p $ toStreamD m
573------------------------------------------------------------------------------
575------------------------------------------------------------------------------
577-- | @insertBy cmp elem stream@ inserts @elem@ before the first element in
578-- @stream@ that is less than @elem@ when compared using @cmp@.
580-- >>> insertBy cmp x = Stream.mergeBy cmp (Stream.fromPure x)
582-- >>> input = Stream.unfold Unfold.fromList [1,3,5]
583-- >>> Stream.fold Fold.toList $ Stream.insertBy compare 2 input
586{-# INLINE insertBy #-}
587insertBy ::Monad m => (a -> a -> Ordering) -> a -> Stream m a -> Stream m a
588insertBy cmp x m = fromStreamD $ D.insertBy cmp x (toStreamD m)
590-- | Insert a pure value between successive elements of a stream.
592-- >>> input = Stream.unfold Unfold.fromList "hello"
593-- >>> Stream.fold Fold.toList $ Stream.intersperse ',' input
596{-# INLINE intersperse #-}
597intersperse :: Monad m => a -> Stream m a -> Stream m a
598intersperse a = fromStreamD . D.intersperse a . toStreamD
600-- | Insert a side effect before consuming an element of a stream except the
603-- >>> input = Stream.unfold Unfold.fromList "hello"
604-- >>> Stream.fold Fold.drain $ Stream.trace putChar $ Stream.intersperseM_ (putChar '.') input
608{-# INLINE intersperseM_ #-}
609intersperseM_ :: Monad m => m b -> Stream m a -> Stream m a
610intersperseM_ m = fromStreamD . D.intersperseM_ m . toStreamD
612-- | Intersperse a monadic action into the input stream after every @n@
615-- >> input = Stream.unfold Unfold.fromList "hello"
616-- >> Stream.fold Fold.toList $ Stream.intersperseBySpan 2 (return ',') input
620{-# INLINE intersperseBySpan #-}
621intersperseBySpan :: -- Monad m =>
622 Int -> m a -> t m a -> t m a
623intersperseBySpan _n _f _xs = undefined
625-- | Insert an effect and its output after consuming an element of a stream.
627-- >>> input = Stream.unfold Unfold.fromList "hello"
628-- >>> Stream.fold Fold.toList $ Stream.trace putChar $ intersperseSuffix (putChar '.' >> return ',') input
629-- h.,e.,l.,l.,o.,"h,e,l,l,o,"
632{-# INLINE intersperseSuffix #-}
633intersperseSuffix :: Monad m => m a -> Stream m a -> Stream m a
634intersperseSuffix m = fromStreamD . D.intersperseSuffix m . toStreamD
636-- | Insert a side effect after consuming an element of a stream.
638-- >>> input = Stream.unfold Unfold.fromList "hello"
639-- >>> Stream.fold Fold.toList $ Stream.intersperseSuffix_ (threadDelay 1000000) input
644{-# INLINE intersperseSuffix_ #-}
645intersperseSuffix_ :: Monad m => m b -> Stream m a -> Stream m a
646intersperseSuffix_ m = fromStreamD . D.intersperseSuffix_ m . toStreamD
648-- XXX Use an offset argument, like tapOffsetEvery
650-- | Like 'intersperseSuffix' but intersperses an effectful action into the
651-- input stream after every @n@ elements and after the last element.
653-- >>> input = Stream.unfold Unfold.fromList "hello"
654-- >>> Stream.fold Fold.toList $ Stream.intersperseSuffixBySpan 2 (return ',') input
659{-# INLINE intersperseSuffixBySpan #-}
660intersperseSuffixBySpan :: Monad m
661 => Int -> m a -> Stream m a -> Stream m a
662intersperseSuffixBySpan n eff =
663 fromStreamD . D.intersperseSuffixBySpan n eff . toStreamD
665-- | Insert a side effect before consuming an element of a stream.
667-- >>> input = Stream.unfold Unfold.fromList "hello"
668-- >>> Stream.fold Fold.toList $ Stream.trace putChar $ Stream.interspersePrefix_ (putChar '.' >> return ',') input
675{-# INLINE interspersePrefix_ #-}
676interspersePrefix_ :: Monad m => m b -> Stream m a -> Stream m a
677interspersePrefix_ m = mapM (\x -> void m >> return x)
679------------------------------------------------------------------------------
681------------------------------------------------------------------------------
683-- | Introduce a delay of specified seconds between elements of the stream.
685-- >>> input = Stream.unfold Unfold.enumerateFromTo (1, 3)
686-- >>> Stream.fold (Fold.drainBy print) $ Stream.delay 1 input
692delay :: MonadIO m => Double -> Stream m a -> Stream m a
693delay n = intersperseM_ $ liftIO $ threadDelay $ round $ n * 1000000
695-- | Introduce a delay of specified seconds after consuming an element of a
698-- >>> input = Stream.unfold Unfold.enumerateFromTo (1, 3)
699-- >>> Stream.fold (Fold.drainBy print) $ Stream.delayPost 1 input
706{-# INLINE delayPost #-}
707delayPost :: MonadIO m => Double -> Stream m a -> Stream m a
708delayPost n = intersperseSuffix_ $ liftIO $ threadDelay $ round $ n * 1000000
710-- | Introduce a delay of specified seconds before consuming an element of a
713-- >>> input = Stream.unfold Unfold.enumerateFromTo (1, 3)
714-- >>> Stream.fold (Fold.drainBy print) $ Stream.delayPre 1 input
721{-# INLINE delayPre #-}
722delayPre :: MonadIO m => Double -> Stream m a -> Stream m a
723delayPre n = trace_ $ liftIO $ threadDelay $ round $ n * 1000000
725------------------------------------------------------------------------------
726-- Reorder in sequence
727------------------------------------------------------------------------------
729-- | Buffer until the next element in sequence arrives. The function argument
730-- determines the difference in sequence numbers. This could be useful in
731-- implementing sequenced streams, for example, TCP reassembly.
735{-# INLINE reassembleBy #-}
742reassembleBy = undefined
744------------------------------------------------------------------------------
746------------------------------------------------------------------------------
749-- >>> f = Fold.foldl' (\(i, _) x -> (i + 1, x)) (-1,undefined)
750-- >>> indexed = Stream.postscan f
751-- >>> indexed = Stream.zipWith (,) (Stream.unfold Unfold.enumerateFrom 0)
753-- Pair each element in a stream with its index, starting from index 0.
755-- >>> Stream.fold Fold.toList $ Stream.indexed $ Stream.unfold Unfold.fromList "hello"
756-- [(0,'h'),(1,'e'),(2,'l'),(3,'l'),(4,'o')]
758{-# INLINE indexed #-}
759indexed :: Monad m => Stream m a -> Stream m (Int, a)
760indexed = fromStreamD . D.indexed . toStreamD
763-- >>> f n = Fold.foldl' (\(i, _) x -> (i - 1, x)) (n + 1,undefined)
764-- >>> indexedR n = Stream.postscan (f n)
766-- >>> s n = Stream.unfold Unfold.enumerateFromThen (n, (n - 1))
767-- >>> indexedR n = Stream.zipWith (,) (s n)
769-- Pair each element in a stream with its index, starting from the
770-- given index @n@ and counting down.
772-- >>> Stream.fold Fold.toList $ Stream.indexedR 10 $ Stream.unfold Unfold.fromList "hello"
773-- [(10,'h'),(9,'e'),(8,'l'),(7,'l'),(6,'o')]
775{-# INLINE indexedR #-}
776indexedR :: Monad m => Int -> Stream m a -> Stream m (Int, a)
777indexedR n = fromStreamD . D.indexedR n . toStreamD
779------------------------------------------------------------------------------
781------------------------------------------------------------------------------
783-- | Find all the indices where the value of the element in the stream is equal
784-- to the given value.
786-- >>> elemIndices a = Stream.findIndices (== a)
788{-# INLINE elemIndices #-}
789elemIndices :: (Monad m, Eq a) => a -> Stream m a -> Stream m Int
790elemIndices a = findIndices (== a)
792------------------------------------------------------------------------------
794------------------------------------------------------------------------------
796-- XXX this is not a one-to-one map so calling it map may not be right.
797-- We can perhaps call it zipWithTail or rollWith.
799-- | Apply a function on every two successive elements of a stream. The first
800-- argument of the map function is the previous element and the second argument
801-- is the current element. When the current element is the first element, the
802-- previous element is 'Nothing'.
806{-# INLINE rollingMap #-}
807rollingMap :: Monad m => (Maybe a -> a -> b) -> Stream m a -> Stream m b
808rollingMap f m = fromStreamD $ D.rollingMap f $ toStreamD m
810-- | Like 'rollingMap' but with an effectful map function.
814{-# INLINE rollingMapM #-}
815rollingMapM :: Monad m => (Maybe a -> a -> m b) -> Stream m a -> Stream m b
816rollingMapM f m = fromStreamD $ D.rollingMapM f $ toStreamD m
818-- | Like 'rollingMap' but requires at least two elements in the stream,
819-- returns an empty stream otherwise.
821-- This is the stream equivalent of the list idiom @zipWith f xs (tail xs)@.
825{-# INLINE rollingMap2 #-}
826rollingMap2 :: Monad m => (a -> a -> b) -> Stream m a -> Stream m b
827rollingMap2 f m = fromStreamD $ D.rollingMap2 f $ toStreamD m
829------------------------------------------------------------------------------
831------------------------------------------------------------------------------
833-- | Map a 'Maybe' returning function to a stream, filter out the 'Nothing'
834-- elements, and return a stream of values extracted from 'Just'.
838-- >>> mapMaybe f = fmap fromJust . Stream.filter isJust . fmap f
840{-# INLINE mapMaybe #-}
841mapMaybe :: Monad m => (a -> Maybe b) -> Stream m a -> Stream m b
842mapMaybe f m = fromStreamD $ D.mapMaybe f $ toStreamD m
844-- | Like 'mapMaybe' but maps a monadic function.
848-- >>> mapMaybeM f = fmap fromJust . Stream.filter isJust . Stream.mapM f
850{-# INLINE_EARLY mapMaybeM #-}
852 => (a -> m (Maybe b)) -> Stream m a -> Stream m b
853mapMaybeM f = fmap fromJust . filter isJust . mapM f
855-- | In a stream of 'Maybe's, discard 'Nothing's and unwrap 'Just's.
859{-# INLINE catMaybes #-}
860catMaybes :: Monad m => Stream m (Maybe a) -> Stream m a
861catMaybes = fmap fromJust . filter isJust
863------------------------------------------------------------------------------
865------------------------------------------------------------------------------
867-- | Discard 'Right's and unwrap 'Left's in an 'Either' stream.
872lefts :: Monad m => Stream m (Either a b) -> Stream m a
873lefts = fmap (fromLeft undefined) . filter isLeft
875-- | Discard 'Left's and unwrap 'Right's in an 'Either' stream.
880rights :: Monad m => Stream m (Either a b) -> Stream m b
881rights = fmap (fromRight undefined) . filter isRight
883-- | Remove the either wrapper and flatten both lefts and as well as rights in
889both :: Monad m => Stream m (Either a a) -> Stream m a
890both = fmap (either id id)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list