Skip to content

Commit 1e2488f

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

File tree

4 files changed

+66
-152
lines changed

4 files changed

+66
-152
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
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

Lines changed: 30 additions & 42 deletions
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
[ () ]
@@ -392,23 +364,27 @@ let[@tail_mod_cons] rec unfold f seed =
392364

393365
[@@@endif]
394366

395-
let rec compare f l1 l2 =
367+
[@@@iflt 4.12]
368+
369+
let rec compare cmp l1 l2 =
396370
match l1, l2 with
397371
| [], [] -> 0
398372
| _, [] -> 1
399373
| [], _ -> -1
400374
| x1 :: l1', x2 :: l2' ->
401-
let c = f x1 x2 in
375+
let c = cmp x1 x2 in
402376
if c <> 0 then
403377
c
404378
else
405-
compare f l1' l2'
379+
compare cmp l1' l2'
406380

407-
let rec equal f l1 l2 =
381+
let rec equal eq l1 l2 =
408382
match l1, l2 with
409383
| [], [] -> true
410384
| [], _ | _, [] -> false
411-
| x1 :: l1', x2 :: l2' -> f x1 x2 && equal f l1' l2'
385+
| x1 :: l1', x2 :: l2' -> eq x1 x2 && equal eq l1' l2'
386+
387+
[@@@endif]
412388

413389
[@@@iflt 5.1]
414390

@@ -969,6 +945,8 @@ let find_pred_exn p l =
969945
| None -> raise Not_found
970946
| Some x -> x
971947
948+
[@@@iflt 5.1]
949+
972950
let find_mapi f l =
973951
let rec aux f i = function
974952
| [] -> None
@@ -979,8 +957,14 @@ let find_mapi f l =
979957
in
980958
aux f 0 l
981959
960+
[@@@endif]
961+
962+
[@@@iflt 4.10]
963+
982964
let find_map f l = find_mapi (fun _ -> f) l
983965
966+
[@@@endif]
967+
984968
let find_idx p l =
985969
find_mapi
986970
(fun i x ->
@@ -999,6 +983,8 @@ let remove ~eq x l =
999983
in
1000984
remove' eq x [] l
1001985
986+
[@@@iflt 5.1]
987+
1002988
let filter_map f l =
1003989
let rec recurse acc l =
1004990
match l with
@@ -1013,6 +999,8 @@ let filter_map f l =
1013999
in
10141000
recurse [] l
10151001
1002+
[@@@endif]
1003+
10161004
let keep_some l = filter_map (fun x -> x) l
10171005
10181006
let keep_ok l =
@@ -1215,6 +1203,9 @@ let inter ~eq l1 l2 =
12151203
in
12161204
inter eq [] l1 l2
12171205
1206+
[@@@iflt 5.1]
1207+
1208+
(* Because our map is tail rec between 4.13 and 5.1 *)
12181209
let mapi f l =
12191210
let r = ref 0 in
12201211
map
@@ -1224,6 +1215,8 @@ let mapi f l =
12241215
y)
12251216
l
12261217
1218+
[@@@endif]
1219+
12271220
let iteri f l =
12281221
let rec aux f i l =
12291222
match l with
@@ -1547,11 +1540,6 @@ let to_string ?(start = "") ?(stop = "") ?(sep = ", ") item_to_string l =
15471540
15481541
let to_iter l k = List.iter k l
15491542
1550-
let rec to_seq l () =
1551-
match l with
1552-
| [] -> Seq.Nil
1553-
| x :: tl -> Seq.Cons (x, to_seq tl)
1554-
15551543
let of_iter i =
15561544
let l = ref [] in
15571545
i (fun x -> l := x :: !l);

src/core/CCList.mli

Lines changed: 16 additions & 56 deletions
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,29 @@ 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+
446+
[@@@iflt 5.1]
447+
456448
val find_mapi : (int -> 'a -> 'b option) -> 'a t -> 'b option
457449
(** [find_mapi f l] is like {!find_map}, but also pass the index to the predicate function.
458450
@since 0.11 *)
459451

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

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-
475464
val keep_some : 'a option t -> 'a t
476465
(** [keep_some l] retains only elements of the form [Some x].
477466
Like [filter_map CCFun.id].
@@ -574,16 +563,6 @@ val group_succ : eq:('a -> 'a -> bool) -> 'a list -> 'a list list
574563

575564
(** {2 Indices} *)
576565

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-
587566
val iteri2 : (int -> 'a -> 'b -> unit) -> 'a t -> 'b t -> unit
588567
(** [iteri2 f l1 l2] applies [f] to the two lists [l1] and [l2] simultaneously.
589568
The integer passed to [f] indicates the index of element.
@@ -758,14 +737,6 @@ val assoc_opt : eq:('a -> 'a -> bool) -> 'a -> ('a * 'b) t -> 'b option
758737
@since 1.5, but only
759738
@since 2.0 with labels *)
760739

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-
769740
val mem_assoc : ?eq:('a -> 'a -> bool) -> 'a -> ('a * _) t -> bool
770741
(** [mem_assoc ?eq k alist] returns [true] iff [k] is a key in [alist].
771742
Like [Assoc.mem].
@@ -884,11 +855,6 @@ val to_iter : 'a t -> 'a iter
884855
(** [to_iter l] returns a [iter] of the elements of the list [l].
885856
@since 2.8 *)
886857

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-
892858
val of_iter : 'a iter -> 'a t
893859
(** [of_iter iter] builds a list from a given [iter].
894860
In the result, elements appear in the same order as they did in the source [iter].
@@ -899,12 +865,6 @@ val of_seq_rev : 'a Seq.t -> 'a t
899865
Renamed from [to_std_seq_rev] since 3.0.
900866
@since 3.0 *)
901867

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-
908868
val to_gen : 'a t -> 'a gen
909869
(** [to_gen l] returns a [gen] of the elements of the list [l]. *)
910870

0 commit comments

Comments
 (0)