From e0a95a3c3104d0e9a68059da63f3e481e1a7d234 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Mon, 7 Jul 2014 17:12:49 +0200 Subject: [PATCH] cons, snoc, take_while, drop_while, doubleton --- sequence.ml | 18 +++++++++++++++++- sequence.mli | 18 +++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/sequence.ml b/sequence.ml index 496ea08..7973aa7 100644 --- a/sequence.ml +++ b/sequence.ml @@ -52,6 +52,11 @@ let singleton x k = k x let return x k = k x 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 *) 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) 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 *) let drop n seq = let count = ref 0 in fun k -> seq (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. *) let rev seq = 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_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) diff --git a/sequence.mli b/sequence.mli index 7b3af7a..767e913 100644 --- a/sequence.mli +++ b/sequence.mli @@ -76,6 +76,16 @@ val empty : 'a t val singleton : 'a -> 'a t (** 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 (** Synonym to {!singleton} *) @@ -84,7 +94,7 @@ val pure : 'a -> 'a t val repeat : 'a -> 'a t (** 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 (** [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 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 (** 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 (** Reverse the sequence. O(n) memory and time, needs the sequence to be finite. The result is persistent and does