add several functions: product2, find, mem;

fix doc error on product
This commit is contained in:
Simon Cruanes 2014-07-01 16:00:09 +02:00
parent 54025273dd
commit a40866e9ec
2 changed files with 39 additions and 9 deletions

View file

@ -304,12 +304,15 @@ let sort_uniq ?(cmp=Pervasives.compare) seq =
uniq ~eq:(fun x y -> cmp x y = 0) seq' uniq ~eq:(fun x y -> cmp x y = 0) seq'
(** Cartesian product of the sequences. *) (** Cartesian product of the sequences. *)
let product outer inner = let product outer inner k =
let inner = persistent inner in
from_iter
(fun k ->
outer (fun x -> outer (fun x ->
inner (fun y -> k (x,y)))) inner (fun y -> k (x,y))
)
let product2 outer inner k =
outer (fun x ->
inner (fun y -> k x y)
)
(** [join ~join_row a b] combines every element of [a] with every (** [join ~join_row a b] combines every element of [a] with every
element of [b] using [join_row]. If [join_row] returns None, then element of [b] using [join_row]. If [join_row] returns None, then
@ -394,6 +397,19 @@ let exists p seq =
false false
with ExitSequence -> true with ExitSequence -> true
let mem ?(eq=(=)) x seq = exists (eq x) seq
let find f seq =
let r = ref None in
begin try
seq (fun x -> match f x with
| None -> ()
| Some _ as res -> r := res
);
with ExitSequence -> ()
end;
!r
(** How long is the sequence? *) (** How long is the sequence? *)
let length seq = let length seq =
let r = ref 0 in let r = ref 0 in

View file

@ -120,6 +120,15 @@ val for_all : ('a -> bool) -> 'a t -> bool
val exists : ('a -> bool) -> 'a t -> bool val exists : ('a -> bool) -> 'a t -> bool
(** Exists there some element satisfying the predicate? *) (** Exists there some element satisfying the predicate? *)
val mem : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> bool
(** Is the value a member of the sequence?
@param eq the equality predicate to use (default [(=)])
@since NEXT_VERSION *)
val find : ('a -> 'b option) -> 'a t -> 'b option
(** Find the first element on which the function doesn't return [None]
@since NEXT_VERSION *)
val length : 'a t -> int val length : 'a t -> int
(** How long is the sequence? Forces the sequence. *) (** How long is the sequence? Forces the sequence. *)
@ -192,9 +201,14 @@ val uniq : ?eq:('a -> 'a -> bool) -> 'a t -> 'a t
like [fun seq -> map List.hd (group seq)]. *) like [fun seq -> map List.hd (group seq)]. *)
val product : 'a t -> 'b t -> ('a * 'b) t val product : 'a t -> 'b t -> ('a * 'b) t
(** Cartesian product of the sequences. The first one is transformed (** Cartesian product of the sequences. When calling [product a b],
by calling [persistent] on it, so that it can be traversed the caller {b MUST} ensure that [b] can be traversed as many times
several times (outer loop of the product) *) as required (several times), possibly by calling {!persistent} on it
beforehand. *)
val product2 : 'a t -> 'b t -> ('a, 'b) t2
(** Binary version of {!product}. Same requirements.
@since NEXT_VERSION *)
val join : join_row:('a -> 'b -> 'c option) -> 'a t -> 'b t -> 'c t val join : join_row:('a -> 'b -> 'c option) -> 'a t -> 'b t -> 'c t
(** [join ~join_row a b] combines every element of [a] with every (** [join ~join_row a b] combines every element of [a] with every