CCOpt.sequence_l

This commit is contained in:
Simon Cruanes 2014-06-26 15:10:13 +02:00
parent 9caefc0e5e
commit ac35980c8b
2 changed files with 16 additions and 0 deletions

View file

@ -90,6 +90,20 @@ let fold f acc o = match o with
| None -> acc
| Some x -> f acc x
let sequence_l l =
let rec aux acc l = match l with
| [] -> Some (List.rev acc)
| Some x :: l' -> aux (x::acc) l'
| None :: _ -> raise Exit
in
try aux [] l with Exit -> None
(*$T
sequence_l [None; Some 1; Some 2] = None
sequence_l [Some 1; Some 2; Some 3] = Some [1;2;3]
sequence_l [] = Some []
*)
let to_list o = match o with
| None -> []
| Some x -> [x]

View file

@ -60,6 +60,8 @@ val iter : ('a -> unit) -> 'a t -> unit
val fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a
(** Fold on 0 or 1 elements *)
val sequence_l : 'a t list -> 'a list t
(** {2 Applicative} *)
val (<*>) : ('a -> 'b) t -> 'a t -> 'b t