Skip to content

Commit 273ba69

Browse files
committed
CCList(cleanup): clean functions that are in the stdlib
1 parent 4781e0b commit 273ba69

File tree

4 files changed

+61
-152
lines changed

4 files changed

+61
-152
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Changelog
22

33
## main
4+
- breaking: CCListLabel.compare and CCListLabel.equal takes the function on the elements as named arguments
5+
- breaking: CCListLabel.init now takes the length as a named arguments to follow the Stdlib
46
- breaking: invert the argument of CCFun.compose to align it with the Stdlib
57
- breaking: change the semantic of CCFloat.{min,max} with respect to NaN to follow the Stdlib
68
- breaking: change the semantic of CCInt.rem with respect to negative number to follow the Stdlib

src/core/CCList.ml

+27-42
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,15 @@
1-
(* backport new functions from stdlib here *)
2-
3-
[@@@ocaml.warning "-32"]
4-
5-
let rec compare_lengths l1 l2 =
6-
match l1, l2 with
7-
| [], [] -> 0
8-
| [], _ :: _ -> -1
9-
| _ :: _, [] -> 1
10-
| _ :: tail1, _ :: tail2 -> compare_lengths tail1 tail2
11-
12-
let rec compare_length_with l n =
13-
match l, n with
14-
| _ when n < 0 -> 1
15-
| [], 0 -> 0
16-
| [], _ -> -1
17-
| _ :: tail, _ -> compare_length_with tail (n - 1)
18-
19-
let rec assoc_opt x = function
20-
| [] -> None
21-
| (y, v) :: _ when Stdlib.( = ) x y -> Some v
22-
| _ :: tail -> assoc_opt x tail
23-
24-
let rec assq_opt x = function
25-
| [] -> None
26-
| (y, v) :: _ when Stdlib.( == ) x y -> Some v
27-
| _ :: tail -> assq_opt x tail
28-
29-
[@@@ocaml.warning "+32"]
30-
31-
(* end of backport *)
32-
331
include List
342

353
let empty = []
364

5+
[@@@iflt 5.1]
6+
377
let is_empty = function
388
| [] -> true
399
| _ :: _ -> false
4010

11+
[@@@endif]
12+
4113
let mguard c =
4214
if c then
4315
[ () ]
@@ -391,25 +363,27 @@ let[@tail_mod_cons] rec unfold f seed =
391363
| Some (v, next) -> v :: unfold f next
392364

393365
[@@@endif]
366+
[@@@iflt 4.12]
394367

395-
let rec compare f l1 l2 =
368+
let rec compare cmp l1 l2 =
396369
match l1, l2 with
397370
| [], [] -> 0
398371
| _, [] -> 1
399372
| [], _ -> -1
400373
| x1 :: l1', x2 :: l2' ->
401-
let c = f x1 x2 in
374+
let c = cmp x1 x2 in
402375
if c <> 0 then
403376
c
404377
else
405-
compare f l1' l2'
378+
compare cmp l1' l2'
406379

407-
let rec equal f l1 l2 =
380+
let rec equal eq l1 l2 =
408381
match l1, l2 with
409382
| [], [] -> true
410383
| [], _ | _, [] -> false
411-
| x1 :: l1', x2 :: l2' -> f x1 x2 && equal f l1' l2'
384+
| x1 :: l1', x2 :: l2' -> eq x1 x2 && equal eq l1' l2'
412385

386+
[@@@endif]
413387
[@@@iflt 5.1]
414388

415389
let rec flat_map_kont f l kont =
@@ -969,6 +943,8 @@ let find_pred_exn p l =
969943
| None -> raise Not_found
970944
| Some x -> x
971945
946+
[@@@iflt 5.1]
947+
972948
let find_mapi f l =
973949
let rec aux f i = function
974950
| [] -> None
@@ -979,8 +955,13 @@ let find_mapi f l =
979955
in
980956
aux f 0 l
981957
958+
[@@@endif]
959+
[@@@iflt 4.10]
960+
982961
let find_map f l = find_mapi (fun _ -> f) l
983962
963+
[@@@endif]
964+
984965
let find_idx p l =
985966
find_mapi
986967
(fun i x ->
@@ -999,6 +980,8 @@ let remove ~eq x l =
999980
in
1000981
remove' eq x [] l
1001982
983+
[@@@iflt 5.1]
984+
1002985
let filter_map f l =
1003986
let rec recurse acc l =
1004987
match l with
@@ -1013,6 +996,8 @@ let filter_map f l =
1013996
in
1014997
recurse [] l
1015998
999+
[@@@endif]
1000+
10161001
let keep_some l = filter_map (fun x -> x) l
10171002
10181003
let keep_ok l =
@@ -1215,6 +1200,9 @@ let inter ~eq l1 l2 =
12151200
in
12161201
inter eq [] l1 l2
12171202
1203+
[@@@iflt 5.1]
1204+
1205+
(* Because our map is tail rec between 4.13 and 5.1 *)
12181206
let mapi f l =
12191207
let r = ref 0 in
12201208
map
@@ -1224,6 +1212,8 @@ let mapi f l =
12241212
y)
12251213
l
12261214
1215+
[@@@endif]
1216+
12271217
let iteri f l =
12281218
let rec aux f i l =
12291219
match l with
@@ -1547,11 +1537,6 @@ let to_string ?(start = "") ?(stop = "") ?(sep = ", ") item_to_string l =
15471537
15481538
let to_iter l k = List.iter k l
15491539
1550-
let rec to_seq l () =
1551-
match l with
1552-
| [] -> Seq.Nil
1553-
| x :: tl -> Seq.Cons (x, to_seq tl)
1554-
15551540
let of_iter i =
15561541
let l = ref [] in
15571542
i (fun x -> l := x :: !l);

src/core/CCList.mli

+15-56
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ type +'a t = 'a list
1616
val empty : 'a t
1717
(** [empty] is [[]]. *)
1818

19+
[@@@iflt 5.1]
20+
1921
val is_empty : _ t -> bool
2022
(** [is_empty l] returns [true] iff [l = []].
2123
@since 0.11 *)
2224

25+
[@@@endif]
26+
2327
val cons_maybe : 'a option -> 'a t -> 'a t
2428
(** [cons_maybe (Some x) l] is [x :: l].
2529
[cons_maybe None l] is [l].
@@ -127,11 +131,6 @@ val count_true_false : ('a -> bool) -> 'a list -> int * int
127131
that satisfy the predicate [p], and [int2] the number of elements that do not satisfy [p].
128132
@since 2.4 *)
129133

130-
val init : int -> (int -> 'a) -> 'a t
131-
(** [init len f] is [f 0; f 1; …; f (len-1)].
132-
@raise Invalid_argument if len < 0.
133-
@since 0.6 *)
134-
135134
val combine : 'a list -> 'b list -> ('a * 'b) list
136135
(** [combine [a1; …; an] [b1; …; bn]] is [[(a1,b1); …; (an,bn)]].
137136
Transform two lists into a list of pairs.
@@ -161,25 +160,17 @@ val split : ('a * 'b) t -> 'a t * 'b t
161160
@since 1.2, but only
162161
@since 2.2 with labels *)
163162

163+
[@@@iflt 4.12]
164+
164165
val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
165166
(** [compare cmp l1 l2] compares the two lists [l1] and [l2]
166167
using the given comparison function [cmp]. *)
167168

168-
val compare_lengths : 'a t -> 'b t -> int
169-
(** [compare_lengths l1 l2] compare the lengths of the two lists [l1] and [l2].
170-
Equivalent to [compare (length l1) (length l2)] but more efficient.
171-
@since 1.5, but only
172-
@since 2.2 with labels *)
173-
174-
val compare_length_with : 'a t -> int -> int
175-
(** [compare_length_with l x] compares the length of the list [l] to an integer [x].
176-
Equivalent to [compare (length l) x] but more efficient.
177-
@since 1.5, but only
178-
@since 2.2 with labels *)
179-
180169
val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
181170
(** [equal p l1 l2] returns [true] if [l1] and [l2] are equal. *)
182171

172+
[@@@endif]
173+
183174
val flat_map : ('a -> 'b t) -> 'a t -> 'b t
184175
(** [flat_map f l] maps and flattens at the same time (safe). Evaluation order is not guaranteed. *)
185176

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

440-
val find_opt : ('a -> bool) -> 'a t -> 'a option
441-
(** [find_opt p l] is the safe version of {!find}.
442-
@since 1.5, but only
443-
@since 2.2 with labels *)
444-
445431
val find_pred_exn : ('a -> bool) -> 'a t -> 'a
446432
(** [find_pred_exn p l] is the unsafe version of {!find_pred}.
447433
@raise Not_found if no such element is found.
448434
@since 0.11 *)
449435

436+
[@@@iflt 4.10]
437+
450438
val find_map : ('a -> 'b option) -> 'a t -> 'b option
451439
(** [find_map f l] traverses [l], applying [f] to each element. If for
452440
some element [x], [f x = Some y], then [Some y] is returned. Otherwise
453441
the call returns [None].
454442
@since 0.11 *)
455443

444+
[@@@endif]
445+
[@@@iflt 5.1]
446+
456447
val find_mapi : (int -> 'a -> 'b option) -> 'a t -> 'b option
457448
(** [find_mapi f l] is like {!find_map}, but also pass the index to the predicate function.
458449
@since 0.11 *)
459450

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

470-
val filter_map : ('a -> 'b option) -> 'a t -> 'b t
471-
(** [filter_map f l] is the sublist of [l] containing only elements for which
472-
[f] returns [Some e].
473-
Map and remove elements at the same time. *)
474-
475463
val keep_some : 'a option t -> 'a t
476464
(** [keep_some l] retains only elements of the form [Some x].
477465
Like [filter_map CCFun.id].
@@ -574,16 +562,6 @@ val group_succ : eq:('a -> 'a -> bool) -> 'a list -> 'a list list
574562

575563
(** {2 Indices} *)
576564

577-
val mapi : (int -> 'a -> 'b) -> 'a t -> 'b t
578-
(** [mapi f l] is like {!map}, but the function [f] is applied to the index of
579-
the element as first argument (counting from 0), and the element
580-
itself as second argument. *)
581-
582-
val iteri : (int -> 'a -> unit) -> 'a t -> unit
583-
(** [iteri f l] is like {!val-iter}, but the function [f] is applied to the index of
584-
the element as first argument (counting from 0), and the element
585-
itself as second argument. *)
586-
587565
val iteri2 : (int -> 'a -> 'b -> unit) -> 'a t -> 'b t -> unit
588566
(** [iteri2 f l1 l2] applies [f] to the two lists [l1] and [l2] simultaneously.
589567
The integer passed to [f] indicates the index of element.
@@ -758,14 +736,6 @@ val assoc_opt : eq:('a -> 'a -> bool) -> 'a -> ('a * 'b) t -> 'b option
758736
@since 1.5, but only
759737
@since 2.0 with labels *)
760738

761-
val assq_opt : 'a -> ('a * 'b) t -> 'b option
762-
(** [assq_opt k alist] returns [Some v] if the given key [k] is present into [alist].
763-
Like [Assoc.assoc_opt] but use physical equality instead of structural equality
764-
to compare keys.
765-
Safe version of {!assq}.
766-
@since 1.5, but only
767-
@since 2.0 with labels *)
768-
769739
val mem_assoc : ?eq:('a -> 'a -> bool) -> 'a -> ('a * _) t -> bool
770740
(** [mem_assoc ?eq k alist] returns [true] iff [k] is a key in [alist].
771741
Like [Assoc.mem].
@@ -884,11 +854,6 @@ val to_iter : 'a t -> 'a iter
884854
(** [to_iter l] returns a [iter] of the elements of the list [l].
885855
@since 2.8 *)
886856

887-
val to_seq : 'a t -> 'a Seq.t
888-
(** [to_seq l] returns a [Seq.t] of the elements of the list [l].
889-
Renamed from [to_std_seq] since 3.0.
890-
@since 3.0 *)
891-
892857
val of_iter : 'a iter -> 'a t
893858
(** [of_iter iter] builds a list from a given [iter].
894859
In the result, elements appear in the same order as they did in the source [iter].
@@ -899,12 +864,6 @@ val of_seq_rev : 'a Seq.t -> 'a t
899864
Renamed from [to_std_seq_rev] since 3.0.
900865
@since 3.0 *)
901866

902-
val of_seq : 'a Seq.t -> 'a t
903-
(** [of_seq seq] builds a list from a given [Seq.t].
904-
In the result, elements appear in the same order as they did in the source [Seq.t].
905-
Renamed from [of_std_seq] since 3.0.
906-
@since 3.0 *)
907-
908867
val to_gen : 'a t -> 'a gen
909868
(** [to_gen l] returns a [gen] of the elements of the list [l]. *)
910869

0 commit comments

Comments
 (0)