aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/option.ml6
-rw-r--r--lib/option.mli3
2 files changed, 9 insertions, 0 deletions
diff --git a/lib/option.ml b/lib/option.ml
index 2a530b89bd..942fff48ae 100644
--- a/lib/option.ml
+++ b/lib/option.ml
@@ -97,6 +97,12 @@ let fold_right f x a =
| Some y -> f y a
| _ -> a
+(** [fold_map f a x] is [a, f y] if [x] is [Some y], and [a] otherwise. *)
+let fold_map f a x =
+ match x with
+ | Some y -> let a, z = f a y in a, Some z
+ | _ -> a, None
+
(** [cata f a x] is [a] if [x] is [None] and [f y] if [x] is [Some y]. *)
let cata f a = function
| Some c -> f c
diff --git a/lib/option.mli b/lib/option.mli
index 8002a7ea29..ef2e311a62 100644
--- a/lib/option.mli
+++ b/lib/option.mli
@@ -66,6 +66,9 @@ val fold_left2 : ('a -> 'b -> 'c -> 'a) -> 'a -> 'b option -> 'c option -> 'a
(** [fold_right f x a] is [f y a] if [x] is [Some y], and [a] otherwise. *)
val fold_right : ('a -> 'b -> 'b) -> 'a option -> 'b -> 'b
+(** [fold_map f a x] is [a, f y] if [x] is [Some y], and [a] otherwise. *)
+val fold_map : ('a -> 'b -> 'a * 'c) -> 'a -> 'b option -> 'a * 'c option
+
(** [cata e f x] is [e] if [x] is [None] and [f a] if [x] is [Some a] *)
val cata : ('a -> 'b) -> 'b -> 'a option -> 'b