add Lazy_list.filter

This commit is contained in:
Simon Cruanes 2016-04-25 19:52:57 +02:00
parent 24add9d4f7
commit 560cba9754
2 changed files with 16 additions and 0 deletions

View file

@ -40,6 +40,18 @@ let rec map ~f l =
| lazy (Cons (x,tl)) -> Cons (f x, map ~f tl) | 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 = let rec append a b =
lazy ( lazy (
match a with match a with

View file

@ -31,6 +31,10 @@ val head : 'a t -> ('a * 'a t) option
val map : f:('a -> 'b) -> 'a t -> 'b t val map : f:('a -> 'b) -> 'a t -> 'b t
(** Lazy map *) (** Lazy map *)
val filter : f:('a -> bool) -> 'a t -> 'a t
(** Filter values
@since NEXT_RELEASE *)
val append : 'a t -> 'a t -> 'a t val append : 'a t -> 'a t -> 'a t
(** Lazy concatenation *) (** Lazy concatenation *)