From 8ee07ed6a2aa58d35e9e842c9a8906b0f1b9d06f Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Tue, 5 Feb 2013 09:57:09 +0100 Subject: [PATCH] cycle and repeat operators directly in Sequence --- sequence.ml | 8 ++++++-- sequence.mli | 12 +++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/sequence.ml b/sequence.ml index 45a8d7a..a64429f 100644 --- a/sequence.ml +++ b/sequence.ml @@ -33,6 +33,12 @@ let from_iter f = f 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 *) let iter f seq = seq f @@ -205,8 +211,6 @@ module Int = let range ~start ~stop = fun k -> for i = start to stop do k i done - - let repeat i = fun k -> while true do k i; done end (** Iterate on sets. The functor must be instantiated with a set type *) diff --git a/sequence.mli b/sequence.mli index 3b15b08..5040b8d 100644 --- a/sequence.mli +++ b/sequence.mli @@ -39,6 +39,13 @@ val from_iter : (('a -> unit) -> unit) -> 'a t val singleton : 'a -> 'a t (** 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} *) val iter : ('a -> unit) -> 'a t -> unit @@ -153,11 +160,6 @@ module Int : sig val range : start:int -> stop:int -> int t (** 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 (** Iterate on sets. The functor must be instantiated with a set type *)