mirror of
https://github.com/c-cube/iter.git
synced 2025-12-06 11:15:32 -05:00
cycle and repeat operators directly in Sequence
This commit is contained in:
parent
10df563bd8
commit
8ee07ed6a2
2 changed files with 13 additions and 7 deletions
|
|
@ -33,6 +33,12 @@ let from_iter f = f
|
||||||
|
|
||||||
let singleton x = fun k -> k x
|
let singleton x = fun k -> k x
|
||||||
|
|
||||||
|
(** Infinite sequence of the same element *)
|
||||||
|
let repeat x = fun k -> while true do k x done
|
||||||
|
|
||||||
|
(** Cycle forever through the given sequence. O(n). *)
|
||||||
|
let cycle s = fun k -> while true do s k; done
|
||||||
|
|
||||||
(** Consume the sequence, passing all its arguments to the function *)
|
(** Consume the sequence, passing all its arguments to the function *)
|
||||||
let iter f seq = seq f
|
let iter f seq = seq f
|
||||||
|
|
||||||
|
|
@ -205,8 +211,6 @@ module Int =
|
||||||
let range ~start ~stop =
|
let range ~start ~stop =
|
||||||
fun k ->
|
fun k ->
|
||||||
for i = start to stop do k i done
|
for i = start to stop do k i done
|
||||||
|
|
||||||
let repeat i = fun k -> while true do k i; done
|
|
||||||
end
|
end
|
||||||
|
|
||||||
(** Iterate on sets. The functor must be instantiated with a set type *)
|
(** Iterate on sets. The functor must be instantiated with a set type *)
|
||||||
|
|
|
||||||
12
sequence.mli
12
sequence.mli
|
|
@ -39,6 +39,13 @@ val from_iter : (('a -> unit) -> unit) -> 'a t
|
||||||
val singleton : 'a -> 'a t
|
val singleton : 'a -> 'a t
|
||||||
(** Singleton sequence *)
|
(** Singleton sequence *)
|
||||||
|
|
||||||
|
val repeat : 'a -> 'a t
|
||||||
|
(** Infinite sequence of the same element *)
|
||||||
|
|
||||||
|
val cycle : 'a t -> 'a t
|
||||||
|
(** Cycle forever through the given sequence. Assume the
|
||||||
|
given sequence can be traversed any amount of times (not transient). *)
|
||||||
|
|
||||||
(** {2 Consume a sequence} *)
|
(** {2 Consume a sequence} *)
|
||||||
|
|
||||||
val iter : ('a -> unit) -> 'a t -> unit
|
val iter : ('a -> unit) -> 'a t -> unit
|
||||||
|
|
@ -153,11 +160,6 @@ module Int :
|
||||||
sig
|
sig
|
||||||
val range : start:int -> stop:int -> int t
|
val range : start:int -> stop:int -> int t
|
||||||
(** Iterator on [start...stop] by steps 1 *)
|
(** Iterator on [start...stop] by steps 1 *)
|
||||||
|
|
||||||
val repeat : int -> int t
|
|
||||||
(** Infinite sequence of integers. Should be used only with
|
|
||||||
transformers such as [take], that work even with infinite
|
|
||||||
sequences. *)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
(** Iterate on sets. The functor must be instantiated with a set type *)
|
(** Iterate on sets. The functor must be instantiated with a set type *)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue