length and is_empty functions for Sequence

This commit is contained in:
Simon Cruanes 2013-02-05 16:40:24 +01:00
parent 855dd739e1
commit 438159aa90
2 changed files with 17 additions and 0 deletions

View file

@ -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)

View file

@ -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