mirror of
https://github.com/c-cube/iter.git
synced 2025-12-06 11:15:32 -05:00
sequence type
This commit is contained in:
commit
4982630967
2 changed files with 51 additions and 0 deletions
31
sequence.ml
Normal file
31
sequence.ml
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
|
||||
(** {2 Transient iterators, that abstract on a finite sequence of elements. *)
|
||||
|
||||
(** Sequence abstract iterator type *)
|
||||
type 'a sequence = {
|
||||
seq_fun: ('a -> unit) -> unit;
|
||||
}
|
||||
|
||||
(** Build a sequence from a iter function *)
|
||||
let from_iter f = {
|
||||
seq_fun = f;
|
||||
}
|
||||
|
||||
(** Consume the sequence, passing all its arguments to the function *)
|
||||
let iter f seq = seq.seq_fun f
|
||||
|
||||
(** Fold over elements of the sequence, consuming it *)
|
||||
let fold f init seq =
|
||||
let r = ref init in
|
||||
seq.seq_fun (fun elt -> r := f !r elt);
|
||||
!r
|
||||
|
||||
(** Map objects of the sequence into other elements, lazily *)
|
||||
let map f seq =
|
||||
let seq_fun' k = seq.seq_fun (fun x -> k (f x)) in
|
||||
{ seq_fun=seq_fun'; }
|
||||
|
||||
(** Filter on elements of the sequence *)
|
||||
let filter p seq =
|
||||
let seq_fun' k = seq.seq_fun (fun x -> if p x then k x) in
|
||||
{ seq_fun=seq_fun'; }
|
||||
20
sequence.mli
Normal file
20
sequence.mli
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
|
||||
(** {2 Transient iterators, that abstract on a finite sequence of elements. *)
|
||||
|
||||
type 'a sequence
|
||||
(** Sequence abstract iterator type *)
|
||||
|
||||
val from_iter : (('a -> unit) -> unit) -> 'a sequence
|
||||
(** Build a sequence from a iter function *)
|
||||
|
||||
val iter : ('a -> unit) -> 'a sequence -> unit
|
||||
(** Consume the sequence, passing all its arguments to the function *)
|
||||
|
||||
val fold : ('b -> 'a -> 'b) -> 'b -> 'a sequence -> 'b
|
||||
(** Fold over elements of the sequence, consuming it *)
|
||||
|
||||
val map : ('a -> 'b) -> 'a sequence -> 'b sequence
|
||||
(** Map objects of the sequence into other elements, lazily *)
|
||||
|
||||
val filter : ('a -> bool) -> 'a sequence -> 'a sequence
|
||||
(** Filter on elements of the sequence *)
|
||||
Loading…
Add table
Reference in a new issue