diff --git a/sequence.ml b/sequence.ml index a64429f..34f26bc 100644 --- a/sequence.ml +++ b/sequence.ml @@ -120,6 +120,17 @@ let exists p seq = false with Exit -> true +(** How long is the sequence? *) +let length seq = + let r = ref 0 in + seq (fun _ -> incr r); + !r + +(** Is the sequence empty? *) +let is_empty seq = + try seq (fun _ -> raise Exit); true + with Exit -> false + module List = struct let of_seq seq = List.rev (fold (fun y x -> x::y) [] seq) diff --git a/sequence.mli b/sequence.mli index 5040b8d..ad44c57 100644 --- a/sequence.mli +++ b/sequence.mli @@ -66,6 +66,12 @@ val for_all : ('a -> bool) -> 'a t -> bool val exists : ('a -> bool) -> 'a t -> bool (** Exists there some element satisfying the predicate? *) +val length : 'a t -> int + (** How long is the sequence? *) + +val is_empty : 'a t -> bool + (** Is the sequence empty? *) + (** {2 Transform a sequence} *) val filter : ('a -> bool) -> 'a t -> 'a t