Modulepipes-4.3.16Haskell2010
Pipes.Prelude
General purpose utilities
The names in this module clash heavily with the Haskell Prelude, so I recommend the following import scheme:
import Pipes
import qualified Pipes.Prelude as P -- or use any other qualifier you preferNote that String-based IO is inefficient. The String-based utilities
in this module exist only for simple demonstrations without incurring a
dependency on the text package.
Also, stdinLn and stdoutLn remove and add newlines, respectively. This
behavior is intended to simplify examples. The corresponding stdin and
stdout utilities from pipes-bytestring and pipes-text preserve
newlines.
- 63 values
- Packagepipes-4.3.16
- Exports63
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourcePrelude.hs
Producers
6 declarationsUse for loops to iterate over Producers whenever you want to perform the same action for every element:
-- Echo all lines from standard input to standard output
runEffect $ for P.stdinLn $ \str -> do
lift $ putStrLn str... or more concisely:
runEffect $ for P.stdinLn (lift . putStrLn)Test<Enter>TestABC<Enter>ABC...
Repeat a monadic action a fixed number of times, yielding each result
replicateM 0 x = return ()
replicateM (m + n) x = replicateM m x >> replicateM n x -- 0 <= {m,n}replicateM :: Monad m => Int -> m a -> Producer a m ()
The natural unfold into a Producer with a step function and a seed
unfoldr next = idConsumers
6 declarationsFeed a Consumer the same value repeatedly using (>~):
runEffect $ lift getLine >~ P.stdoutLnTest<Enter>TestABC<Enter>ABC...
Consume all values using a monadic function
discard all incoming values
Pipes
22 declarationsUse (>->) to connect Producers, Pipes, and Consumers:
runEffect $ P.stdinLn >-> P.takeWhile (/= "quit") >-> P.stdoutLnTest<Enter>TestABC<Enter>ABCquit<Enter>
Apply a function to all values flowing downstream
map id = cat
map (g . f) = map f >-> map gApply a monadic function to all values flowing downstream
mapM return = cat
mapM (f >=> g) = mapM f >-> mapM gConvert a stream of actions to a stream of values
Apply a function to all values flowing downstream, and forward each element of the result.
(filter predicate) only forwards values that satisfy the predicate.
filter (pure True) = cat
filter (liftA2 (&&) p1 p2) = filter p1 >-> filter p2
filter f = mapMaybe (\a -> a <$ guard (f a))(mapMaybe f) yields Just results of f.
Basic laws:
mapMaybe (f >=> g) = mapMaybe f >-> mapMaybe g
mapMaybe (pure @Maybe . f) = mapMaybe (Just . f) = map f
mapMaybe (const Nothing) = drainAs a result of the second law,
mapMaybe return = mapMaybe Just = cat(filterM predicate) only forwards values that satisfy the monadic
predicate
filterM (pure (pure True)) = cat
filterM (liftA2 (liftA2 (&&)) p1 p2) = filterM p1 >-> filterM p2
filterM f = wither (\a -> (\b -> a <$ guard b) <$> f a)(wither f) forwards Just values produced by the
monadic action.
Basic laws:
wither (runMaybeT . (MaybeT . f >=> MaybeT . g)) = wither f >-> wither g
wither (runMaybeT . lift . f) = wither (fmap Just . f) = mapM f
wither (pure . f) = mapMaybe fAs a result of the second law,
wither (runMaybeT . return) = catAs a result of the third law,
wither (pure . const Nothing) = wither (const (pure Nothing)) = drain(take n) only allows n values to pass through
take 0 = return ()
take (m + n) = take m >> take ntake <infinity> = cat
take (min m n) = take m >-> take n(takeWhile p) allows values to pass downstream so long as they satisfy
the predicate p.
takeWhile (pure True) = cat
takeWhile (liftA2 (&&) p1 p2) = takeWhile p1 >-> takeWhile p2(takeWhile' p) is a version of takeWhile that returns the value failing
the predicate.
takeWhile' (pure True) = cat
takeWhile' (liftA2 (&&) p1 p2) = takeWhile' p1 >-> takeWhile' p2(drop n) discards n values going downstream
drop 0 = cat
drop (m + n) = drop m >-> drop n(dropWhile p) discards values going downstream until one violates the
predicate p.
dropWhile (pure False) = cat
dropWhile (liftA2 (||) p1 p2) = dropWhile p1 >-> dropWhile p2Flatten all Foldable elements flowing downstream
Outputs the indices of all elements that match the given element
Outputs the indices of all elements that satisfied the predicate
Strict left scan
Control.Foldl.purely scan :: Monad m => Fold a b -> Pipe a b m rStrict, monadic left scan
Control.Foldl.impurely scanM :: Monad m => FoldM m a b -> Pipe a b m rApply an action to all values flowing downstream
chain (pure (return ())) = cat
chain (liftA2 (>>) m1 m2) = chain m1 >-> chain m2Parse Readable values, only forwarding the value if the parse succeeds
Evaluate all values flowing downstream to WHNF
ListT
1 declarationFolds
24 declarationsUse these to fold the output of a Producer. Many of these folds will stop drawing elements if they can compute their result early, like any:
P.any Prelude.null P.stdinLnTest<Enter>ABC<Enter><Enter>True
Strict fold of the elements of a Producer
Control.Foldl.purely fold :: Monad m => Fold a b -> Producer a m () -> m bStrict fold of the elements of a Producer that preserves the return value
Control.Foldl.purely fold' :: Monad m => Fold a b -> Producer a m r -> m (b, r)Strict, monadic fold of the elements of a Producer
Control.Foldl.impurely foldM :: Monad m => FoldM a b -> Producer a m () -> m bStrict, monadic fold of the elements of a Producer
Control.Foldl.impurely foldM' :: Monad m => FoldM a b -> Producer a m r -> m (b, r)(all predicate p) determines whether all the elements of p satisfy the
predicate.
(any predicate p) determines whether any element of p satisfies the
predicate.
Determines whether all elements are True
Determines whether any element is True
Find the first element of a Producer that satisfies the predicate
Find the index of the first element of a Producer that satisfies the predicate
Retrieve the first element from a Producer
Index into a Producer
Retrieve the last element from a Producer
Count the number of elements in a Producer
Find the maximum element of a Producer
Find the minimum element of a Producer
Determine if a Producer is empty
Compute the sum of the elements of a Producer
Compute the product of the elements of a Producer
Convert a pure Producer into a list
Zips
2 declarationsZip two Producers
Zip two Producers using the provided combining function