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
18import Streamly.Internal.Data.Fold.Type (Fold)
19import Streamly.Internal.Data.Stream.Type (Stream)
21import qualified Streamly.Internal.Data.Stream.Type as Stream
22import qualified Streamly.Internal.Data.Stream.StreamD.Transform as StreamD
24import Prelude hiding (map, mapM, filter)
29-- >>> import qualified Streamly.Internal.Data.Fold as Fold
30-- >>> import qualified Streamly.Data.Unfold as Unfold
31-- >>> import qualified Streamly.Internal.Data.Stream as Stream
33------------------------------------------------------------------------------
34-- Transformation by Mapping
35------------------------------------------------------------------------------
38-- >> mapM f = Stream.sequence . Stream.map f
40-- Apply a monadic function to each element of the stream and replace it with
41-- the output of the resulting action.
43-- >>> s = Stream.unfold Unfold.fromList ["a", "b", "c"]
44-- >>> Stream.fold Fold.drain $ Stream.mapM putStr s
50mapM :: Monad m => (a -> m b) -> Stream m a -> Stream m b
51mapM f = Stream.fromStreamD . StreamD.mapM f . Stream.toStreamD
58-- >>> s = Stream.unfold Unfold.fromList [1,2,3]
59-- >>> Stream.fold Fold.toList $ Stream.map (+1) s
65map :: Monad m => (a -> b) -> Stream m a -> Stream m b
66map f = mapM (return . f)
68-- | Include only those elements that pass a predicate.
71filter :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
72filter p = Stream.fromStreamD . StreamD.filter p . Stream.toStreamD
74-- | Use a filtering fold on a stream.
77-- Stream.fold Fold.sum
78-- $ Stream.foldFilter (Fold.maybe (\x -> if x > 5 then Just x else Nothing))
79-- $ Stream.unfold Unfold.fromList [1..10]
85{-# INLINE foldFilter #-}
86foldFilter :: Monad m => Fold m a (Maybe b) -> Stream m a -> Stream m b
87foldFilter p = Stream.fromStreamD . StreamD.foldFilter p . Stream.toStreamD
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