diff options
author | 2015-06-24 13:48:08 +0200 | |
---|---|---|
committer | 2016-06-14 06:21:30 +0200 | |
commit | 4962e042f3b2d7c5b089cec2dfe4e07a46bd2231 (patch) | |
tree | 37938138823eefd70e2f36d6e05405f8bd2de70f /lib | |
parent | 0f2b25d8b89a5ef3f3824b1840d97dd79a287d0e (diff) |
Add a [CList.partitioni] function.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/cList.ml | 11 | ||||
-rw-r--r-- | lib/cList.mli | 1 |
2 files changed, 12 insertions, 0 deletions
diff --git a/lib/cList.ml b/lib/cList.ml index ba592d13f..3a792d416 100644 --- a/lib/cList.ml +++ b/lib/cList.ml @@ -47,6 +47,8 @@ sig ('a -> 'b -> 'c -> 'd -> 'e) -> 'a list -> 'b list -> 'c list -> 'd list -> 'e list val filteri : (int -> 'a -> bool) -> 'a list -> 'a list + val partitioni : + (int -> 'a -> bool) -> 'a list -> 'a list * 'a list val smartfilter : ('a -> bool) -> 'a list -> 'a list val extend : bool list -> 'a -> 'a list -> 'a list val count : ('a -> bool) -> 'a list -> int @@ -486,6 +488,15 @@ let filteri p = in filter_i_rec 0 +let partitioni p = + let rec aux i = function + | [] -> [], [] + | x :: l -> + let (l1, l2) = aux (succ i) l in + if p i x then (x :: l1, l2) + else (l1, x :: l2) + in aux 0 + let rec sep_last = function | [] -> failwith "sep_last" | hd::[] -> (hd,[]) diff --git a/lib/cList.mli b/lib/cList.mli index 9c7b815c1..b19d1a80f 100644 --- a/lib/cList.mli +++ b/lib/cList.mli @@ -89,6 +89,7 @@ sig val map4 : ('a -> 'b -> 'c -> 'd -> 'e) -> 'a list -> 'b list -> 'c list -> 'd list -> 'e list val filteri : (int -> 'a -> bool) -> 'a list -> 'a list + val partitioni : (int -> 'a -> bool) -> 'a list -> 'a list * 'a list val smartfilter : ('a -> bool) -> 'a list -> 'a list (** [smartfilter f [a1...an] = List.filter f [a1...an]] but if for all i |