mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 03:05:28 -05:00
CCOpt.sequence_l
This commit is contained in:
parent
9caefc0e5e
commit
ac35980c8b
2 changed files with 16 additions and 0 deletions
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue