add CCList.{remove,is_empty}

This commit is contained in:
Simon Cruanes 2015-05-24 21:28:07 +02:00
parent 4b704d9d1a
commit 262a2baacc
2 changed files with 26 additions and 0 deletions

View file

@ -30,6 +30,10 @@ type 'a t = 'a list
let empty = []
let is_empty = function
| [] -> true
| _::_ -> false
(* max depth for direct recursion *)
let direct_depth_default_ = 1000
@ -422,6 +426,19 @@ let find_idx p l = find_mapi (fun i x -> if p x then Some (i, x) else None) l
find (fun x -> if x=3 then Some "a" else None) [1;2;4;5] = None
*)
let remove ?(eq=(=)) ~x l =
let rec remove' eq x acc l = match l with
| [] -> List.rev acc
| y :: tail when eq x y -> remove' eq x acc tail
| y :: tail -> remove' eq x (y::acc) tail
in
remove' eq x [] l
(*$T
remove ~x:1 [2;1;3;3;2;1] = [2;3;3;2]
remove ~x:10 [1;2;3] = [1;2;3]
*)
let filter_map f l =
let rec recurse acc l = match l with
| [] -> List.rev acc

View file

@ -30,6 +30,10 @@ type 'a t = 'a list
val empty : 'a t
val is_empty : _ t -> bool
(** [is_empty l] returns [true] iff [l = []]
@since NEXT_RELEASE *)
val map : ('a -> 'b) -> 'a t -> 'b t
(** Safe version of map *)
@ -140,6 +144,11 @@ val find_idx : ('a -> bool) -> 'a t -> (int * 'a) option
(** [find p x] returns [Some (i,x)] where [x] is the [i]-th element of [l],
and [p x] holds. Otherwise returns [None] *)
val remove : ?eq:('a -> 'a -> bool) -> x:'a -> 'a t -> 'a t
(** [remove ~x l] removes every instance of [x] from [l]. Tailrec.
@param eq equality function
@since NEXT_RELEASE *)
val filter_map : ('a -> 'b option) -> 'a t -> 'b t
(** Map and remove elements at the same time *)