aboutsummaryrefslogtreecommitdiff
path: root/lib/cList.ml
diff options
context:
space:
mode:
authorGuillaume Melquiond2015-12-26 10:07:19 +0100
committerGuillaume Melquiond2015-12-31 17:01:51 +0100
commit1a157442dff4bfa127af467c49280e79889acde7 (patch)
tree7d10a0093e75b652c7cce79920c054d4c2f41c91 /lib/cList.ml
parent1a8a8db7a9e4eb5ab56cd192411529661a4972c7 (diff)
Do not compose List.length with List.filter.
Diffstat (limited to 'lib/cList.ml')
-rw-r--r--lib/cList.ml7
1 files changed, 7 insertions, 0 deletions
diff --git a/lib/cList.ml b/lib/cList.ml
index 0ac372d8d8..72f892a09d 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)