From c3e0f81f7ebab9d3843dafd40f23de938494c73a Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Mon, 25 Apr 2016 20:39:08 +0200 Subject: [PATCH] add `CCLazy_list.take` --- src/iter/CCLazy_list.ml | 9 +++++++++ src/iter/CCLazy_list.mli | 6 +++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/iter/CCLazy_list.ml b/src/iter/CCLazy_list.ml index a1d94cf0..a258f372 100644 --- a/src/iter/CCLazy_list.ml +++ b/src/iter/CCLazy_list.ml @@ -50,8 +50,17 @@ let filter ~f l = (*$= [2;4;6] (of_list [1;2;3;4;5;6;7] |> filter ~f:(fun x -> x mod 2=0) |> to_list) + [2;4;6] (of_gen Gen.(1 -- max_int) |> filter ~f:(fun x -> x mod 2=0) |> take 3 |> to_list) *) +let rec take n l = + lazy ( + match l with + | _ when n=0 -> Nil + | lazy Nil -> Nil + | lazy (Cons (x,tl)) -> Cons (x, take (n-1) tl) + ) + let rec append a b = lazy ( match a with diff --git a/src/iter/CCLazy_list.mli b/src/iter/CCLazy_list.mli index 77c7870d..613be3fe 100644 --- a/src/iter/CCLazy_list.mli +++ b/src/iter/CCLazy_list.mli @@ -32,7 +32,11 @@ val map : f:('a -> 'b) -> 'a t -> 'b t (** Lazy map *) val filter : f:('a -> bool) -> 'a t -> 'a t -(** Filter values +(** Filter values. + @since NEXT_RELEASE *) + +val take : int -> 'a t -> 'a t +(** Take at most n values. @since NEXT_RELEASE *) val append : 'a t -> 'a t -> 'a t