mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
add CCList.{remove,is_empty}
This commit is contained in:
parent
4b704d9d1a
commit
262a2baacc
2 changed files with 26 additions and 0 deletions
|
|
@ -30,6 +30,10 @@ type 'a t = 'a list
|
||||||
|
|
||||||
let empty = []
|
let empty = []
|
||||||
|
|
||||||
|
let is_empty = function
|
||||||
|
| [] -> true
|
||||||
|
| _::_ -> false
|
||||||
|
|
||||||
(* max depth for direct recursion *)
|
(* max depth for direct recursion *)
|
||||||
let direct_depth_default_ = 1000
|
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
|
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 filter_map f l =
|
||||||
let rec recurse acc l = match l with
|
let rec recurse acc l = match l with
|
||||||
| [] -> List.rev acc
|
| [] -> List.rev acc
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,10 @@ type 'a t = 'a list
|
||||||
|
|
||||||
val empty : 'a t
|
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
|
val map : ('a -> 'b) -> 'a t -> 'b t
|
||||||
(** Safe version of map *)
|
(** 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],
|
(** [find p x] returns [Some (i,x)] where [x] is the [i]-th element of [l],
|
||||||
and [p x] holds. Otherwise returns [None] *)
|
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
|
val filter_map : ('a -> 'b option) -> 'a t -> 'b t
|
||||||
(** Map and remove elements at the same time *)
|
(** Map and remove elements at the same time *)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue