add Option.flat_map_l

This commit is contained in:
Simon Cruanes 2023-04-07 20:30:43 -04:00
parent d985019fe1
commit 9261e654e7
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 9 additions and 0 deletions

View file

@ -46,6 +46,11 @@ let[@inline] flat_map f o =
| None -> None | None -> None
| Some x -> f x | Some x -> f x
let[@inline] flat_map_l f o =
match o with
| None -> []
| Some x -> f x
let[@inline] bind o f = flat_map f o let[@inline] bind o f = flat_map f o
let ( >>= ) = bind let ( >>= ) = bind
let pure x = Some x let pure x = Some x

View file

@ -49,6 +49,10 @@ val flat_map : ('a -> 'b t) -> 'a t -> 'b t
(** [flat_map f o] is equivalent to {!map} followed by {!flatten}. (** [flat_map f o] is equivalent to {!map} followed by {!flatten}.
Flip version of {!(>>=)}. *) Flip version of {!(>>=)}. *)
val flat_map_l : ('a -> 'b list) -> 'a t -> 'b list
(** [flat_map_l f o] is [[]] if [o] is [None], or [f x] if [o] is [Some x].
@since NEXT_RELEASE *)
val bind : 'a t -> ('a -> 'b t) -> 'b t val bind : 'a t -> ('a -> 'b t) -> 'b t
(** [bind o f] is [f v] if [o] is [Some v], [None] otherwise. (** [bind o f] is [f v] if [o] is [Some v], [None] otherwise.
Monadic bind. Monadic bind.