mapM
Map each element of a structure to a monadic action, evaluate these
actions from left to right, and collect the results. For a version
that ignores the results see
mapM_.
Examples
mapM is literally a
traverse with a type signature
restricted to
Monad. Its implementation may be more efficient
due to additional power of
Monad.
Apply a monadic transformation to all values in a stream.
If you do not need the transformed values, and instead just want the
monadic side-effects of running the action, see
mapM_.
Subject to fusion
Apply a monadic transformation to all values in a stream.
If you do not need the transformed values, and instead just want the
monadic side-effects of running the action, see
mapM_.
Subject to fusion
Since 0.3.0
Apply a monadic operation to each element of a
Stream, lazily
Apply a monadic function to all values flowing downstream
mapM return = cat
mapM (f >=> g) = mapM f >-> mapM g
Replace each element of a stream with the result of a monadic action
>>> S.print $ S.mapM readIORef $ S.chain (\ior -> modifyIORef ior (*100)) $ S.mapM newIORef $ each [1..6]
100
200
300
400
500
600
See also
chain for a variant of this which ignores the return
value of the function and just uses the side effects.
Map each element of a structure to a monadic action, evaluate these
actions from left to right, and collect the results. For a version
that ignores the results see
mapM_.
O(n) Apply the monadic action to all elements of the vector,
yielding a vector of results
O(n) Apply the monadic action to all elements of the vector,
yielding a vector of results
O(n) Apply the monadic action to all elements of the vector,
yielding a vector of results
O(n) Apply the monadic action to all elements of the vector,
yielding a vector of results
mapM f = sequence . map f
Apply a monadic function to each element of the stream and replace it
with the output of the resulting action.
>>> drain $ Stream.mapM putStr $ Stream.fromList ["a", "b", "c"]
abc
>>> :{
drain $ Stream.replicateM 10 (return 1)
& (fromSerial . Stream.mapM (x -> threadDelay 1000000 >> print x))
:}
1
...
1
> drain $ Stream.replicateM 10 (return 1)
& (fromAsync . Stream.mapM (x -> threadDelay 1000000 >> print x))
Concurrent (do not use with fromParallel on infinite
streams)
Map each element of a structure to a monadic action, evaluate these
actions from left to right, and collect the results. for a version
that ignores the results see
mapM_.
Like
mapM, but applying the function to the individual list
items in parallel.
O(n) Apply the monadic action to all elements of the vector,
yielding a vector of results.
O(n) Apply the monadic action to all elements of the vector,
yielding a vector of results.
O(n) Apply the monadic action to all elements of the vector,
yielding a vector of results.