From bc4089316632cb11f0ae7a07559c92b94cf16e75 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 11 Jun 2014 21:54:01 +0200 Subject: [PATCH] CCList.filter --- core/CCList.ml | 13 +++++++++++++ core/CCList.mli | 3 +++ 2 files changed, 16 insertions(+) diff --git a/core/CCList.ml b/core/CCList.ml index f6eac495..1eb68c32 100644 --- a/core/CCList.ml +++ b/core/CCList.ml @@ -61,6 +61,19 @@ let append l1 l2 = let (@) = append +let filter p l = + let rec direct i p l = match l with + | [] -> [] + | _ when i=0 -> safe p l [] + | x::l' when not (p x) -> direct i p l' + | x::l' -> x :: direct (i-1) p l' + and safe p l acc = match l with + | [] -> List.rev acc + | x::l' when not (p x) -> safe p l' acc + | x::l' -> safe p l' (x::acc) + in + direct _direct_depth p l + let fold_right f l acc = let rec direct i f l acc = match l with | [] -> acc diff --git a/core/CCList.mli b/core/CCList.mli index f06284f1..0d0901c5 100644 --- a/core/CCList.mli +++ b/core/CCList.mli @@ -36,6 +36,9 @@ val append : 'a t -> 'a t -> 'a t val (@) : 'a t -> 'a t -> 'a t +val filter : ('a -> bool) -> 'a t -> 'a t +(** Safe version of {!List.filter} *) + val fold_right : ('a -> 'b -> 'b) -> 'a t -> 'b -> 'b (** Safe version of [fold_right] *)