From 560cba975425ecd67786e812dccf33c446d81490 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Mon, 25 Apr 2016 19:52:57 +0200 Subject: [PATCH] add `Lazy_list.filter` --- src/iter/CCLazy_list.ml | 12 ++++++++++++ src/iter/CCLazy_list.mli | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/src/iter/CCLazy_list.ml b/src/iter/CCLazy_list.ml index ffaf76ce..a1d94cf0 100644 --- a/src/iter/CCLazy_list.ml +++ b/src/iter/CCLazy_list.ml @@ -40,6 +40,18 @@ let rec map ~f l = | lazy (Cons (x,tl)) -> Cons (f x, map ~f tl) ) +let filter ~f l = + let rec aux f l = match l with + | lazy Nil -> Nil + | lazy (Cons (x,tl)) when f x -> Cons (x, lazy (aux f tl)) + | lazy (Cons (_, tl)) -> aux f tl + in + lazy (aux f l) + +(*$= + [2;4;6] (of_list [1;2;3;4;5;6;7] |> filter ~f:(fun x -> x mod 2=0) |> to_list) +*) + let rec append a b = lazy ( match a with diff --git a/src/iter/CCLazy_list.mli b/src/iter/CCLazy_list.mli index 6a51cd3b..77c7870d 100644 --- a/src/iter/CCLazy_list.mli +++ b/src/iter/CCLazy_list.mli @@ -31,6 +31,10 @@ val head : 'a t -> ('a * 'a t) option val map : f:('a -> 'b) -> 'a t -> 'b t (** Lazy map *) +val filter : f:('a -> bool) -> 'a t -> 'a t +(** Filter values + @since NEXT_RELEASE *) + val append : 'a t -> 'a t -> 'a t (** Lazy concatenation *)