on master help sign in
Merge pull request #3877 from clayrat/patch-3

Add foldlM

commitea65347ce820986c7dbd58c3cf2f153e27115e9e authorNiklas Larsson <metaniklas@gmail.com> dateWed 28 Jun 2017, 00:21 (9 years ago) parent463076b43cd347
1 file changed+6−1
libs/prelude/Prelude/Monad.idr+6−1
libs/prelude/Prelude/Monad.idr+6−1
@@ -6,6 +6,7 @@ import Builtins
66import Prelude.Functor
77import public Prelude.Applicative
88import Prelude.Basics
9+import Prelude.Foldable
910import IO
1011
1112%access public export
@@ -33,6 +34,11 @@ flatten : Monad m => m (m a) -> m a
3334flatten = join
3435%deprecate flatten "Please use `join`, which is the standard name."
3536
37+||| Similar to `foldl`, but uses a function wrapping its result in a `Monad`.
38+||| Consequently, the final value is wrapped in the same `Monad`.
39+foldlM : (Foldable t, Monad m) => (funcM: a -> b -> m a) -> (init: a) -> (input: t b) -> m a
40+foldlM fm a0 = foldl (\ma,b => ma >>= flip fm b) (pure a0)
41+
3642-- Annoyingly, these need to be here, so that we can use them in other
3743-- Prelude modules other than the top level.
3844
@@ -48,4 +54,3 @@ Applicative (IO' ffi) where
4854
4955Monad (IO' ffi) where
5056 b >>= k = io_bind b k
51−