diff options
| author | Arnaud Spiwack | 2014-02-27 14:50:40 +0100 |
|---|---|---|
| committer | Arnaud Spiwack | 2014-02-27 14:50:40 +0100 |
| commit | 0dfc87c68d5aba5adac079cb62ae504d82b303b9 (patch) | |
| tree | fb6b99101cab4609188ad74ca8684434a7d19cea /lib | |
| parent | d98fdf1ef5789f6d5420e52c34a33debf08584e9 (diff) | |
Tacinterp: refactoring using Monad.
Adds a combinator List.map_right which chains effects from right to left.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/monad.ml | 24 | ||||
| -rw-r--r-- | lib/monad.mli | 6 |
2 files changed, 29 insertions, 1 deletions
diff --git a/lib/monad.ml b/lib/monad.ml index 773f952def..a1508ae01c 100644 --- a/lib/monad.ml +++ b/lib/monad.ml @@ -32,9 +32,14 @@ module type S = sig (** List combinators *) module List : sig - (** [map f l] applies the monadic effect left to right. *) + (** [map f l] maps [f] on the elements of [l] in left to right + order. *) val map : ('a -> 'b t) -> 'a list -> 'b list t + (** [map f l] maps [f] on the elements of [l] in right to left + order. *) + val map_right : ('a -> 'b t) -> 'a list -> 'b list t + end end @@ -53,6 +58,23 @@ module Make (M:Def) : S with type +'a t = 'a M.t = struct map f l >>= fun l' -> return (a'::l') + let rec map_right f = function + | [] -> return [] + | a::l -> + map f l >>= fun l' -> + f a >>= fun a' -> + return (a'::l') + end end + + + + + + + + + + diff --git a/lib/monad.mli b/lib/monad.mli index 1f04cf5e0c..5c4f74e51d 100644 --- a/lib/monad.mli +++ b/lib/monad.mli @@ -32,8 +32,14 @@ module type S = sig (** List combinators *) module List : sig + (** [map f l] maps [f] on the elements of [l] in left to right + order. *) val map : ('a -> 'b t) -> 'a list -> 'b list t + (** [map f l] maps [f] on the elements of [l] in right to left + order. *) + val map_right : ('a -> 'b t) -> 'a list -> 'b list t + end end |
