diff options
author | 2014-10-21 12:21:16 +0200 | |
---|---|---|
committer | 2014-10-22 07:31:45 +0200 | |
commit | e87a491d724c83fe1bc98dfb94c665e8ba7f56a1 (patch) | |
tree | 2ffa0b21a43e26cbb168dbc08ca0460759e03b7a | |
parent | a8ea528113f89302f7156416e1f3da18848e59b2 (diff) |
An additional [List.iter] monadic combinator.
-rw-r--r-- | lib/monad.ml | 13 | ||||
-rw-r--r-- | lib/monad.mli | 5 |
2 files changed, 18 insertions, 0 deletions
diff --git a/lib/monad.ml b/lib/monad.ml index 2d305a3c9..bc58c4d17 100644 --- a/lib/monad.ml +++ b/lib/monad.ml @@ -59,6 +59,11 @@ module type ListS = sig operator calls its second argument in a tail position. *) val fold_left : ('a -> 'b -> 'a t) -> 'a -> 'b list -> 'a t + (** Like the regular [List.iter]. The monadic effects are threaded + left to right. It is tail-recurisve if the [>>] operator calls + its second argument in a tail position. *) + val iter : ('a -> unit t) -> 'a list -> unit t + (** {6 Two-list iterators} *) @@ -132,6 +137,11 @@ module Make (M:Def) : S with type +'a t = 'a M.t = struct f x' b >>= fun x'' -> fold_left f x'' l + let rec iter f = function + | [] -> return () + | [a] -> f a + | a::b::l -> f a >> f b >> iter f l + exception SizeMismatch @@ -148,3 +158,6 @@ module Make (M:Def) : S with type +'a t = 'a M.t = struct end end + + + diff --git a/lib/monad.mli b/lib/monad.mli index d5c5cbc69..a6be1df7a 100644 --- a/lib/monad.mli +++ b/lib/monad.mli @@ -61,6 +61,11 @@ module type ListS = sig operator calls its second argument in a tail position. *) val fold_left : ('a -> 'b -> 'a t) -> 'a -> 'b list -> 'a t + (** Like the regular [List.iter]. The monadic effects are threaded + left to right. It is tail-recurisve if the [>>] operator calls + its second argument in a tail position. *) + val iter : ('a -> unit t) -> 'a list -> unit t + (** {6 Two-list iterators} *) |