mirror of
https://github.com/c-cube/iter.git
synced 2025-12-05 19:00:31 -05:00
length and is_empty functions for Sequence
This commit is contained in:
parent
855dd739e1
commit
438159aa90
2 changed files with 17 additions and 0 deletions
11
sequence.ml
11
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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue