replicateM
replicateM n act performs the action
act
n times, and then returns the list of results:
Examples
>>> import Control.Monad.State
>>> runState (replicateM 3 $ state $ \s -> (s, s + 1)) 1
([1,2,3],4)
O(n) Execute the monadic action the given number of times and
store the results in a vector.
Yield a
Bundle of values obtained by performing the monadic
action the given number of times
O(n) Execute the monadic action the given number of times and
store the results in a vector.
Create a mutable vector of the given length (0 if the length is
negative) and fill it with values produced by repeatedly executing the
monadic action.
Create a mutable vector of the given length (0 if the length is
negative) and fill it with values produced by repeatedly executing the
monadic action.
O(n) Execute the monadic action the given number of times and
store the results in a vector.
Create a mutable vector of the given length (0 if the length is
negative) and fill it with values produced by repeatedly executing the
monadic action.
O(n) Execute the monadic action the given number of times and
store the results in a vector.
Create a mutable vector of the given length (0 if the length is
negative) and fill it with values produced by repeatedly executing the
monadic action.
O(n) Execute the monadic action the given number of times and
store the results in a vector.
Create a mutable vector of the given length (0 if the length is
negative) and fill it with values produced by repeatedly executing the
monadic action.
Perform the given action n times, yielding each result.
Subject to fusion
Replicate a monadic value the given number of times.
Subject to fusion
Since 1.2.0
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 ()
Repeat an action several times, streaming its results.
>>> S.print $ S.replicateM 2 getCurrentTime
2015-08-18 00:57:36.124508 UTC
2015-08-18 00:57:36.124785 UTC
replicateM n act performs the action
n times,
gathering the results.
Using
ApplicativeDo: '
replicateM 5 as' can be
understood as the
do expression
do a1 <- as
a2 <- as
a3 <- as
a4 <- as
a5 <- as
pure [a1,a2,a3,a4,a5]
Note the
Applicative constraint.
>>> replicateM n = Stream.take n . Stream.repeatM
Generate a stream by performing a monadic action
n times.
Same as:
>>> pr n = threadDelay 1000000 >> print n
This runs serially and takes 3 seconds:
>>> Stream.drain $ Stream.fromSerial $ Stream.replicateM 3 $ pr 1
1
1
1
This runs concurrently and takes just 1 second:
>>> Stream.drain $ Stream.fromAsync $ Stream.replicateM 3 $ pr 1
1
1
1
Concurrent
Like
replicateM, but executing the action multiple times in
parallel.
Create a mutable vector where the length is inferred from the type and
fill it with values produced by repeatedly executing the monadic
action.
O(n) Execute the monadic action n times and store the
results in a vector where n is inferred from the type.
Create a mutable vector where the length is inferred from the type and
fill it with values produced by repeatedly executing the monadic
action.
Create a mutable vector where the length is inferred from the type and
fill it with values produced by repeatedly executing the monadic
action.