mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
add a guard function for list comprehensions
This commit is contained in:
parent
26df938968
commit
61a8cc58bd
3 changed files with 30 additions and 0 deletions
|
|
@ -74,6 +74,8 @@ let is_empty = function
|
||||||
| [] -> true
|
| [] -> true
|
||||||
| _::_ -> false
|
| _::_ -> false
|
||||||
|
|
||||||
|
let mguard c = if c then [ () ] else []
|
||||||
|
|
||||||
(* max depth for direct recursion *)
|
(* max depth for direct recursion *)
|
||||||
let direct_depth_default_ = 1000
|
let direct_depth_default_ = 1000
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -322,6 +322,20 @@ val interleave : 'a list -> 'a list -> 'a list
|
||||||
val pure : 'a -> 'a t
|
val pure : 'a -> 'a t
|
||||||
(** [pure x] is [return x]. *)
|
(** [pure x] is [return x]. *)
|
||||||
|
|
||||||
|
val mguard : bool -> unit t
|
||||||
|
(** [mguard c] is [pure ()] if [c] is true, [[]] otherwise.
|
||||||
|
This is useful to define a list by comprehension, e.g.:
|
||||||
|
{[
|
||||||
|
# let square_even xs =
|
||||||
|
let* x = xs in
|
||||||
|
let* () = mguard (x mod 2 = 0) in
|
||||||
|
return @@ x * x;;
|
||||||
|
val square_even : int list -> int list = <fun>
|
||||||
|
# square_even [1;2;4;3;5;2];;
|
||||||
|
- : int list = [4; 16; 4]
|
||||||
|
]}
|
||||||
|
@since 3.1 *)
|
||||||
|
|
||||||
val (<*>) : ('a -> 'b) t -> 'a t -> 'b t
|
val (<*>) : ('a -> 'b) t -> 'a t -> 'b t
|
||||||
(** [funs <*> l] is [product (fun f x -> f x) funs l]. *)
|
(** [funs <*> l] is [product (fun f x -> f x) funs l]. *)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -326,6 +326,20 @@ val interleave : 'a list -> 'a list -> 'a list
|
||||||
val pure : 'a -> 'a t
|
val pure : 'a -> 'a t
|
||||||
(** [pure x] is [return x]. *)
|
(** [pure x] is [return x]. *)
|
||||||
|
|
||||||
|
val mguard : bool -> unit t
|
||||||
|
(** [mguard c] is [pure ()] if [c] is true, [[]] otherwise.
|
||||||
|
This is useful to define a list by comprehension, e.g.:
|
||||||
|
{[
|
||||||
|
# let square_even xs =
|
||||||
|
let* x = xs in
|
||||||
|
let* () = mguard (x mod 2 = 0) in
|
||||||
|
return @@ x * x;;
|
||||||
|
val square_even : int list -> int list = <fun>
|
||||||
|
# square_even [1;2;4;3;5;2];;
|
||||||
|
- : int list = [4; 16; 4]
|
||||||
|
]}
|
||||||
|
@since 3.1 *)
|
||||||
|
|
||||||
val (<*>) : ('a -> 'b) t -> 'a t -> 'b t
|
val (<*>) : ('a -> 'b) t -> 'a t -> 'b t
|
||||||
(** [funs <*> l] is [product (fun f x -> f x) funs l]. *)
|
(** [funs <*> l] is [product (fun f x -> f x) funs l]. *)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue