Skip to content

Commit

Permalink
CCList(cleanup): clean functions that are in the stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
FardaleM committed Dec 25, 2024
1 parent 4781e0b commit 1e2488f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 152 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changelog

## main
- breaking: CCListLabel.compare and CCListLabel.equal takes the function on the elements as named arguments
- breaking: CCListLabel.init now takes the length as a named arguments to follow the Stdlib
- breaking: invert the argument of CCFun.compose to align it with the Stdlib
- breaking: change the semantic of CCFloat.{min,max} with respect to NaN to follow the Stdlib
- breaking: change the semantic of CCInt.rem with respect to negative number to follow the Stdlib
Expand Down
72 changes: 30 additions & 42 deletions src/core/CCList.ml
Original file line number Diff line number Diff line change
@@ -1,43 +1,15 @@
(* backport new functions from stdlib here *)

[@@@ocaml.warning "-32"]

let rec compare_lengths l1 l2 =
match l1, l2 with
| [], [] -> 0
| [], _ :: _ -> -1
| _ :: _, [] -> 1
| _ :: tail1, _ :: tail2 -> compare_lengths tail1 tail2

let rec compare_length_with l n =
match l, n with
| _ when n < 0 -> 1
| [], 0 -> 0
| [], _ -> -1
| _ :: tail, _ -> compare_length_with tail (n - 1)

let rec assoc_opt x = function
| [] -> None
| (y, v) :: _ when Stdlib.( = ) x y -> Some v
| _ :: tail -> assoc_opt x tail

let rec assq_opt x = function
| [] -> None
| (y, v) :: _ when Stdlib.( == ) x y -> Some v
| _ :: tail -> assq_opt x tail

[@@@ocaml.warning "+32"]

(* end of backport *)

include List

let empty = []

[@@@iflt 5.1]

let is_empty = function
| [] -> true
| _ :: _ -> false

[@@@endif]

let mguard c =
if c then
[ () ]
Expand Down Expand Up @@ -392,23 +364,27 @@ let[@tail_mod_cons] rec unfold f seed =

[@@@endif]

let rec compare f l1 l2 =
[@@@iflt 4.12]

let rec compare cmp l1 l2 =
match l1, l2 with
| [], [] -> 0
| _, [] -> 1
| [], _ -> -1
| x1 :: l1', x2 :: l2' ->
let c = f x1 x2 in
let c = cmp x1 x2 in
if c <> 0 then
c
else
compare f l1' l2'
compare cmp l1' l2'

let rec equal f l1 l2 =
let rec equal eq l1 l2 =
match l1, l2 with
| [], [] -> true
| [], _ | _, [] -> false
| x1 :: l1', x2 :: l2' -> f x1 x2 && equal f l1' l2'
| x1 :: l1', x2 :: l2' -> eq x1 x2 && equal eq l1' l2'

[@@@endif]

[@@@iflt 5.1]

Expand Down Expand Up @@ -969,6 +945,8 @@ let find_pred_exn p l =
| None -> raise Not_found
| Some x -> x
[@@@iflt 5.1]
let find_mapi f l =
let rec aux f i = function
| [] -> None
Expand All @@ -979,8 +957,14 @@ let find_mapi f l =
in
aux f 0 l
[@@@endif]
[@@@iflt 4.10]
let find_map f l = find_mapi (fun _ -> f) l
[@@@endif]
let find_idx p l =
find_mapi
(fun i x ->
Expand All @@ -999,6 +983,8 @@ let remove ~eq x l =
in
remove' eq x [] l
[@@@iflt 5.1]
let filter_map f l =
let rec recurse acc l =
match l with
Expand All @@ -1013,6 +999,8 @@ let filter_map f l =
in
recurse [] l
[@@@endif]
let keep_some l = filter_map (fun x -> x) l
let keep_ok l =
Expand Down Expand Up @@ -1215,6 +1203,9 @@ let inter ~eq l1 l2 =
in
inter eq [] l1 l2
[@@@iflt 5.1]
(* Because our map is tail rec between 4.13 and 5.1 *)
let mapi f l =
let r = ref 0 in
map
Expand All @@ -1224,6 +1215,8 @@ let mapi f l =
y)
l
[@@@endif]
let iteri f l =
let rec aux f i l =
match l with
Expand Down Expand Up @@ -1547,11 +1540,6 @@ let to_string ?(start = "") ?(stop = "") ?(sep = ", ") item_to_string l =
let to_iter l k = List.iter k l
let rec to_seq l () =
match l with
| [] -> Seq.Nil
| x :: tl -> Seq.Cons (x, to_seq tl)
let of_iter i =
let l = ref [] in
i (fun x -> l := x :: !l);
Expand Down
72 changes: 16 additions & 56 deletions src/core/CCList.mli
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ type +'a t = 'a list
val empty : 'a t
(** [empty] is [[]]. *)

[@@@iflt 5.1]

val is_empty : _ t -> bool
(** [is_empty l] returns [true] iff [l = []].
@since 0.11 *)

[@@@endif]

val cons_maybe : 'a option -> 'a t -> 'a t
(** [cons_maybe (Some x) l] is [x :: l].
[cons_maybe None l] is [l].
Expand Down Expand Up @@ -127,11 +131,6 @@ val count_true_false : ('a -> bool) -> 'a list -> int * int
that satisfy the predicate [p], and [int2] the number of elements that do not satisfy [p].
@since 2.4 *)

val init : int -> (int -> 'a) -> 'a t
(** [init len f] is [f 0; f 1; …; f (len-1)].
@raise Invalid_argument if len < 0.
@since 0.6 *)

val combine : 'a list -> 'b list -> ('a * 'b) list
(** [combine [a1; …; an] [b1; …; bn]] is [[(a1,b1); …; (an,bn)]].
Transform two lists into a list of pairs.
Expand Down Expand Up @@ -161,25 +160,17 @@ val split : ('a * 'b) t -> 'a t * 'b t
@since 1.2, but only
@since 2.2 with labels *)

[@@@iflt 4.12]

val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
(** [compare cmp l1 l2] compares the two lists [l1] and [l2]
using the given comparison function [cmp]. *)

val compare_lengths : 'a t -> 'b t -> int
(** [compare_lengths l1 l2] compare the lengths of the two lists [l1] and [l2].
Equivalent to [compare (length l1) (length l2)] but more efficient.
@since 1.5, but only
@since 2.2 with labels *)

val compare_length_with : 'a t -> int -> int
(** [compare_length_with l x] compares the length of the list [l] to an integer [x].
Equivalent to [compare (length l) x] but more efficient.
@since 1.5, but only
@since 2.2 with labels *)

val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
(** [equal p l1 l2] returns [true] if [l1] and [l2] are equal. *)

[@@@endif]

val flat_map : ('a -> 'b t) -> 'a t -> 'b t
(** [flat_map f l] maps and flattens at the same time (safe). Evaluation order is not guaranteed. *)

Expand Down Expand Up @@ -437,26 +428,29 @@ val find_pred : ('a -> bool) -> 'a t -> 'a option
or returns [None] if no element satisfies [p].
@since 0.11 *)

val find_opt : ('a -> bool) -> 'a t -> 'a option
(** [find_opt p l] is the safe version of {!find}.
@since 1.5, but only
@since 2.2 with labels *)

val find_pred_exn : ('a -> bool) -> 'a t -> 'a
(** [find_pred_exn p l] is the unsafe version of {!find_pred}.
@raise Not_found if no such element is found.
@since 0.11 *)

[@@@iflt 4.10]

val find_map : ('a -> 'b option) -> 'a t -> 'b option
(** [find_map f l] traverses [l], applying [f] to each element. If for
some element [x], [f x = Some y], then [Some y] is returned. Otherwise
the call returns [None].
@since 0.11 *)

[@@@endif]

[@@@iflt 5.1]

val find_mapi : (int -> 'a -> 'b option) -> 'a t -> 'b option
(** [find_mapi f l] is like {!find_map}, but also pass the index to the predicate function.
@since 0.11 *)

[@@@endif]

val find_idx : ('a -> bool) -> 'a t -> (int * 'a) option
(** [find_idx p x] returns [Some (i,x)] where [x] is the [i]-th element of [l],
and [p x] holds. Otherwise returns [None]. *)
Expand All @@ -467,11 +461,6 @@ val remove : eq:('a -> 'a -> bool) -> key:'a -> 'a t -> 'a t
@since 0.11 *)
(* FIXME: the original CCList.mli uses ~x instead of ~key !! *)

val filter_map : ('a -> 'b option) -> 'a t -> 'b t
(** [filter_map f l] is the sublist of [l] containing only elements for which
[f] returns [Some e].
Map and remove elements at the same time. *)

val keep_some : 'a option t -> 'a t
(** [keep_some l] retains only elements of the form [Some x].
Like [filter_map CCFun.id].
Expand Down Expand Up @@ -574,16 +563,6 @@ val group_succ : eq:('a -> 'a -> bool) -> 'a list -> 'a list list

(** {2 Indices} *)

val mapi : (int -> 'a -> 'b) -> 'a t -> 'b t
(** [mapi f l] is like {!map}, but the function [f] is applied to the index of
the element as first argument (counting from 0), and the element
itself as second argument. *)

val iteri : (int -> 'a -> unit) -> 'a t -> unit
(** [iteri f l] is like {!val-iter}, but the function [f] is applied to the index of
the element as first argument (counting from 0), and the element
itself as second argument. *)

val iteri2 : (int -> 'a -> 'b -> unit) -> 'a t -> 'b t -> unit
(** [iteri2 f l1 l2] applies [f] to the two lists [l1] and [l2] simultaneously.
The integer passed to [f] indicates the index of element.
Expand Down Expand Up @@ -758,14 +737,6 @@ val assoc_opt : eq:('a -> 'a -> bool) -> 'a -> ('a * 'b) t -> 'b option
@since 1.5, but only
@since 2.0 with labels *)

val assq_opt : 'a -> ('a * 'b) t -> 'b option
(** [assq_opt k alist] returns [Some v] if the given key [k] is present into [alist].
Like [Assoc.assoc_opt] but use physical equality instead of structural equality
to compare keys.
Safe version of {!assq}.
@since 1.5, but only
@since 2.0 with labels *)

val mem_assoc : ?eq:('a -> 'a -> bool) -> 'a -> ('a * _) t -> bool
(** [mem_assoc ?eq k alist] returns [true] iff [k] is a key in [alist].
Like [Assoc.mem].
Expand Down Expand Up @@ -884,11 +855,6 @@ val to_iter : 'a t -> 'a iter
(** [to_iter l] returns a [iter] of the elements of the list [l].
@since 2.8 *)

val to_seq : 'a t -> 'a Seq.t
(** [to_seq l] returns a [Seq.t] of the elements of the list [l].
Renamed from [to_std_seq] since 3.0.
@since 3.0 *)

val of_iter : 'a iter -> 'a t
(** [of_iter iter] builds a list from a given [iter].
In the result, elements appear in the same order as they did in the source [iter].
Expand All @@ -899,12 +865,6 @@ val of_seq_rev : 'a Seq.t -> 'a t
Renamed from [to_std_seq_rev] since 3.0.
@since 3.0 *)

val of_seq : 'a Seq.t -> 'a t
(** [of_seq seq] builds a list from a given [Seq.t].
In the result, elements appear in the same order as they did in the source [Seq.t].
Renamed from [of_std_seq] since 3.0.
@since 3.0 *)

val to_gen : 'a t -> 'a gen
(** [to_gen l] returns a [gen] of the elements of the list [l]. *)

Expand Down
Loading

0 comments on commit 1e2488f

Please sign in to comment.