Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[enhance] list: add filter_stable

  • Loading branch information...
commit 64bea566023dbff15b3e529b9f0f1296844ef190 1 parent 1e56d7a
Mathieu Barbin authored
Showing with 15 additions and 0 deletions.
  1. +8 −0 libbase/baseList.ml
  2. +7 −0 libbase/baseList.mli
View
8 libbase/baseList.ml
@@ -500,6 +500,14 @@ let map_stable map list =
let flist = List.fold_left fct [] list in
if !equal then list else List.rev flist
+let filter_stable filter list =
+ let equal = ref true in
+ let f acc elt =
+ if filter elt then elt :: acc else (equal := false ; acc)
+ in
+ let acc = List.fold_left f [] list in
+ if !equal then list else List.rev acc
+
(**
{[('acc -> 'input -> ('acc * 'output)) -> 'acc -> 'output list -> 'input list -> ('acc * 'output list)]}
@param f takes the accumulator and the head of the list to give back the new element
View
7 libbase/baseList.mli
@@ -303,6 +303,13 @@ val fold_left_snd : ('acc -> 'elt -> 'acc) -> 'acc -> (_ * 'elt) list -> 'acc
*)
val map_stable : ('a -> 'a) -> 'a list -> 'a list
+(**
+ This is like [filter] but returns physically the same list if all
+ elements are kept by the filter function.
+ Tail recursive.
+*)
+val filter_stable : ('a -> bool) -> 'a list -> 'a list
+
(** This is a fold_left combined with a map. Tail recursive. *)
val fold_left_map :
((*accumulator*)'a -> 'b -> 'a * 'c) -> (*init*)'a -> 'b list -> 'a * 'c list
Please sign in to comment.
Something went wrong with that request. Please try again.