mirror of
https://github.com/c-cube/iter.git
synced 2025-12-06 19:25:30 -05:00
cons, snoc, take_while, drop_while, doubleton
This commit is contained in:
parent
75c87b0507
commit
e0a95a3c31
2 changed files with 34 additions and 2 deletions
18
sequence.ml
18
sequence.ml
|
|
@ -52,6 +52,11 @@ let singleton x k = k x
|
||||||
let return x k = k x
|
let return x k = k x
|
||||||
let pure f k = k f
|
let pure f k = k f
|
||||||
|
|
||||||
|
let doubleton x y k = k x; k y
|
||||||
|
|
||||||
|
let cons x l k = k x; l k
|
||||||
|
let snoc l x k = l k; k x
|
||||||
|
|
||||||
(** Infinite sequence of the same element *)
|
(** Infinite sequence of the same element *)
|
||||||
let repeat x = fun k -> while true do k x done
|
let repeat x = fun k -> while true do k x done
|
||||||
|
|
||||||
|
|
@ -381,12 +386,24 @@ let take n seq =
|
||||||
if !count = n then raise ExitSequence)
|
if !count = n then raise ExitSequence)
|
||||||
with ExitSequence -> ()
|
with ExitSequence -> ()
|
||||||
|
|
||||||
|
let take_while p seq k =
|
||||||
|
try
|
||||||
|
seq (fun x -> if p x then k x else raise ExitSequence)
|
||||||
|
with ExitSequence -> ()
|
||||||
|
|
||||||
(** Drop the [n] first elements of the sequence *)
|
(** Drop the [n] first elements of the sequence *)
|
||||||
let drop n seq =
|
let drop n seq =
|
||||||
let count = ref 0 in
|
let count = ref 0 in
|
||||||
fun k -> seq
|
fun k -> seq
|
||||||
(fun x -> if !count >= n then k x else incr count)
|
(fun x -> if !count >= n then k x else incr count)
|
||||||
|
|
||||||
|
let drop_while p seq k =
|
||||||
|
let drop = ref true in
|
||||||
|
seq (fun x ->
|
||||||
|
if !drop
|
||||||
|
then if p x then () else (drop := false; k x)
|
||||||
|
else k x)
|
||||||
|
|
||||||
(** Reverse the sequence. O(n) memory. *)
|
(** Reverse the sequence. O(n) memory. *)
|
||||||
let rev seq =
|
let rev seq =
|
||||||
let l = MList.of_seq seq in
|
let l = MList.of_seq seq in
|
||||||
|
|
@ -476,7 +493,6 @@ let map2_2 f g seq2 =
|
||||||
let to_list seq = List.rev (fold (fun y x -> x::y) [] seq)
|
let to_list seq = List.rev (fold (fun y x -> x::y) [] seq)
|
||||||
|
|
||||||
let to_rev_list seq = fold (fun y x -> x :: y) [] seq
|
let to_rev_list seq = fold (fun y x -> x :: y) [] seq
|
||||||
(** Get the list of the reversed sequence (more efficient) *)
|
|
||||||
|
|
||||||
let of_list l = from_iter (fun k -> List.iter k l)
|
let of_list l = from_iter (fun k -> List.iter k l)
|
||||||
|
|
||||||
|
|
|
||||||
18
sequence.mli
18
sequence.mli
|
|
@ -76,6 +76,16 @@ val empty : 'a t
|
||||||
val singleton : 'a -> 'a t
|
val singleton : 'a -> 'a t
|
||||||
(** Singleton sequence, with exactly one element. *)
|
(** Singleton sequence, with exactly one element. *)
|
||||||
|
|
||||||
|
val doubleton : 'a -> 'a -> 'a t
|
||||||
|
(** Sequence with exactly two elements *)
|
||||||
|
|
||||||
|
val cons : 'a -> 'a t -> 'a t
|
||||||
|
(** [cons x l] yields [x], then yields from [l].
|
||||||
|
Same as [append (singleton x) l] *)
|
||||||
|
|
||||||
|
val snoc : 'a t -> 'a -> 'a t
|
||||||
|
(** Same as {!cons} but yields the element after iterating on [l] *)
|
||||||
|
|
||||||
val return : 'a -> 'a t
|
val return : 'a -> 'a t
|
||||||
(** Synonym to {!singleton} *)
|
(** Synonym to {!singleton} *)
|
||||||
|
|
||||||
|
|
@ -84,7 +94,7 @@ val pure : 'a -> 'a t
|
||||||
|
|
||||||
val repeat : 'a -> 'a t
|
val repeat : 'a -> 'a t
|
||||||
(** Infinite sequence of the same element. You may want to look
|
(** Infinite sequence of the same element. You may want to look
|
||||||
at {!take} if you iterate on it. *)
|
at {!take} and the likes if you iterate on it. *)
|
||||||
|
|
||||||
val iterate : ('a -> 'a) -> 'a -> 'a t
|
val iterate : ('a -> 'a) -> 'a -> 'a t
|
||||||
(** [iterate f x] is the infinite sequence (x, f(x), f(f(x)), ...) *)
|
(** [iterate f x] is the infinite sequence (x, f(x), f(f(x)), ...) *)
|
||||||
|
|
@ -247,9 +257,15 @@ val take : int -> 'a t -> 'a t
|
||||||
(** Take at most [n] elements from the sequence. Works on infinite
|
(** Take at most [n] elements from the sequence. Works on infinite
|
||||||
sequences. *)
|
sequences. *)
|
||||||
|
|
||||||
|
val take_while : ('a -> bool) -> 'a t -> 'a t
|
||||||
|
(** Take elements while they satisfy the predicate, then stops iterating *)
|
||||||
|
|
||||||
val drop : int -> 'a t -> 'a t
|
val drop : int -> 'a t -> 'a t
|
||||||
(** Drop the [n] first elements of the sequence. Lazy. *)
|
(** Drop the [n] first elements of the sequence. Lazy. *)
|
||||||
|
|
||||||
|
val drop_while : ('a -> bool) -> 'a t -> 'a t
|
||||||
|
(** Predicate version of {!drop} *)
|
||||||
|
|
||||||
val rev : 'a t -> 'a t
|
val rev : 'a t -> 'a t
|
||||||
(** Reverse the sequence. O(n) memory and time, needs the
|
(** Reverse the sequence. O(n) memory and time, needs the
|
||||||
sequence to be finite. The result is persistent and does
|
sequence to be finite. The result is persistent and does
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue