Add utility functions to result

all and concat_map
This commit is contained in:
Jérémie Dimino 2018-02-23 16:03:40 +07:00 committed by Rudi Grinberg
parent e77bd30681
commit 9943acb4dc
2 changed files with 32 additions and 0 deletions

View File

@ -29,3 +29,25 @@ module O = struct
let ( >>= ) t f = bind t ~f
let ( >>| ) t f = map t ~f
end
open O
let all =
let rec loop acc = function
| [] -> Ok (List.rev acc)
| t :: l ->
t >>= fun x ->
loop (x :: acc) l
in
fun l -> loop [] l
let concat_map =
let rec loop f acc = function
| [] -> Ok (List.rev acc)
| x :: l ->
f x >>= fun y ->
loop f (List.rev_append y acc) l
in
fun l ~f -> loop f [] l
type ('a, 'error) result = ('a, 'error) t

View File

@ -16,3 +16,13 @@ val map : ('a, 'error) t -> f:('a -> 'b) -> ('b, 'error) t
val bind : ('a, 'error) t -> f:('a -> ('b, 'error) t) -> ('b, 'error) t
val map_error : ('a, 'error1) t -> f:('error1 -> 'error2) -> ('a, 'error2) t
val all : ('a, 'error) t list -> ('a list, 'error) t
val concat_map
: 'a list
-> f:('a -> ('b list, 'error) t)
-> ('b list, 'error) t
(** For compatibility with some other code *)
type ('a, 'error) result = ('a, 'error) t