diff options
author | zapashcanon <leo@ndrs.fr> | 2019-10-29 09:45:19 +0100 |
---|---|---|
committer | Alain Frisch <alain@frisch.fr> | 2019-10-29 09:45:19 +0100 |
commit | c0e4096eaab43ddc9eb69f241d37969d3adbd23b (patch) | |
tree | 2b7c1a188106e69f46904f3bfe6fd2c5873c4de0 /stdlib | |
parent | 7e668b411ae71106155939167ed6cdb8201dab86 (diff) | |
download | ocaml-c0e4096eaab43ddc9eb69f241d37969d3adbd23b.tar.gz |
add List.filteri to the stdlib (#9059)
Diffstat (limited to 'stdlib')
-rw-r--r-- | stdlib/list.ml | 7 | ||||
-rw-r--r-- | stdlib/list.mli | 7 | ||||
-rw-r--r-- | stdlib/listLabels.mli | 7 |
3 files changed, 21 insertions, 0 deletions
diff --git a/stdlib/list.ml b/stdlib/list.ml index 2b9e545b88..ab43fee436 100644 --- a/stdlib/list.ml +++ b/stdlib/list.ml @@ -244,6 +244,13 @@ let find_all p = let filter = find_all +let filteri p l = + let rec aux i acc = function + | [] -> rev acc + | x::l -> aux (i + 1) (if p i x then x::acc else acc) l + in + aux 0 [] l + let filter_map f = let rec aux accu = function | [] -> rev accu diff --git a/stdlib/list.mli b/stdlib/list.mli index b7b6a89b6a..e60c9bd0a2 100644 --- a/stdlib/list.mli +++ b/stdlib/list.mli @@ -252,6 +252,13 @@ val filter : ('a -> bool) -> 'a list -> 'a list val find_all : ('a -> bool) -> 'a list -> 'a list (** [find_all] is another name for {!List.filter}. *) +val filteri : (int -> 'a -> bool) -> 'a list -> 'a list +(** Same as {!List.filter}, but the predicate is applied to the index of + the element as first argument (counting from 0), and the element + itself as second argument. + @since 4.11.0 +*) + val partition : ('a -> bool) -> 'a list -> 'a list * 'a list (** [partition p l] returns a pair of lists [(l1, l2)], where [l1] is the list of all the elements of [l] that diff --git a/stdlib/listLabels.mli b/stdlib/listLabels.mli index 7004d78909..c19588a147 100644 --- a/stdlib/listLabels.mli +++ b/stdlib/listLabels.mli @@ -266,6 +266,13 @@ val filter : f:('a -> bool) -> 'a list -> 'a list val find_all : f:('a -> bool) -> 'a list -> 'a list (** [find_all] is another name for {!List.filter}. *) +val filteri : f:(int -> 'a -> bool) -> 'a list -> 'a list +(** Same as {!List.filter}, but the predicate is applied to the index of + the element as first argument (counting from 0), and the element + itself as second argument. + @since 4.11.0 +*) + val partition : f:('a -> bool) -> 'a list -> 'a list * 'a list (** [partition p l] returns a pair of lists [(l1, l2)], where [l1] is the list of all the elements of [l] that |