Map a monadic function over a structure, discarding results.
mapM_ is like mapM but discards the results of the monadic actions, returning only the combined effect. Its type signature is (Foldable t, Monad m) => (a -> m b) -> t a -> m (). It is more efficient than mapM when results are not needed.
mapM_ sequences effects from left to right, just like mapM. The difference is that it does not build up a result list, making it suitable for purely side-effecting operations like printing or writing to files.
Use mapM_ when you only care about the effects, not the return values. The flipped variant forM_ is also available. For the applicative version, use traverse_ from Data.Foldable.
| Name | Description | Optional |
|---|---|---|
f |
A function (a -> m b) returning a monadic value. | No |
xs |
A foldable structure to iterate over. | No |
mapM_ print [1, 2, 3] -- prints 1, 2, 3; returns ()
mapM_ putStrLn ["hello", "world"] -- prints each string on a line