aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib
diff options
context:
space:
mode:
authorGravatar Guillaume Melquiond <guillaume.melquiond@inria.fr>2015-12-26 10:07:19 +0100
committerGravatar Guillaume Melquiond <guillaume.melquiond@inria.fr>2015-12-31 17:01:51 +0100
commit1a157442dff4bfa127af467c49280e79889acde7 (patch)
tree7d10a0093e75b652c7cce79920c054d4c2f41c91 /lib
parent1a8a8db7a9e4eb5ab56cd192411529661a4972c7 (diff)
Do not compose List.length with List.filter.
Diffstat (limited to 'lib')
-rw-r--r--lib/cList.ml7
-rw-r--r--lib/cList.mli2
2 files changed, 9 insertions, 0 deletions
diff --git a/lib/cList.ml b/lib/cList.ml
index 0ac372d8d..72f892a09 100644
--- a/lib/cList.ml
+++ b/lib/cList.ml
@@ -48,6 +48,7 @@ sig
val filteri :
(int -> 'a -> bool) -> 'a list -> 'a list
val smartfilter : ('a -> bool) -> 'a list -> 'a list
+ val count : ('a -> bool) -> 'a list -> int
val index : 'a eq -> 'a -> 'a list -> int
val index0 : 'a eq -> 'a -> 'a list -> int
val iteri : (int -> 'a -> unit) -> 'a list -> unit
@@ -375,6 +376,12 @@ let rec smartfilter f l = match l with
else h :: tl'
else tl'
+let count f l =
+ let rec aux acc = function
+ | [] -> acc
+ | h :: t -> if f h then aux (acc + 1) t else aux acc t in
+ aux 0 l
+
let rec index_f f x l n = match l with
| [] -> raise Not_found
| y :: l -> if f x y then n else index_f f x l (succ n)
diff --git a/lib/cList.mli b/lib/cList.mli
index 19eeb2509..1487f67a3 100644
--- a/lib/cList.mli
+++ b/lib/cList.mli
@@ -94,6 +94,8 @@ sig
(** [smartfilter f [a1...an] = List.filter f [a1...an]] but if for all i
[f ai = true], then [smartfilter f l == l] *)
+ val count : ('a -> bool) -> 'a list -> int
+
val index : 'a eq -> 'a -> 'a list -> int
(** [index] returns the 1st index of an element in a list (counting from 1). *)