diff --git a/sequence.ml b/sequence.ml index 37e13c9..f5f3d6f 100644 --- a/sequence.ml +++ b/sequence.ml @@ -304,12 +304,15 @@ let sort_uniq ?(cmp=Pervasives.compare) seq = uniq ~eq:(fun x y -> cmp x y = 0) seq' (** Cartesian product of the sequences. *) -let product outer inner = - let inner = persistent inner in - from_iter - (fun k -> - outer (fun x -> - inner (fun y -> k (x,y)))) +let product outer inner k = + outer (fun x -> + 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 element of [b] using [join_row]. If [join_row] returns None, then @@ -394,6 +397,19 @@ let exists p seq = false 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? *) let length seq = let r = ref 0 in diff --git a/sequence.mli b/sequence.mli index 9266875..6416be8 100644 --- a/sequence.mli +++ b/sequence.mli @@ -120,6 +120,15 @@ val for_all : ('a -> bool) -> 'a t -> bool val exists : ('a -> bool) -> 'a t -> bool (** 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 (** 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)]. *) val product : 'a t -> 'b t -> ('a * 'b) t - (** Cartesian product of the sequences. The first one is transformed - by calling [persistent] on it, so that it can be traversed - several times (outer loop of the product) *) + (** Cartesian product of the sequences. When calling [product a b], + the caller {b MUST} ensure that [b] can be traversed as many times + 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 (** [join ~join_row a b] combines every element of [a] with every