Skip to content

Commit 5ef875b

Browse files
committed
[Belt] Add MakeHashable/MakeHashableU
1 parent cc5c2c6 commit 5ef875b

File tree

3 files changed

+92
-24
lines changed

3 files changed

+92
-24
lines changed

jscomp/others/belt_Id.ml

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,9 @@ let comparableU
7373
end) in
7474
(module N : Comparable with type t = key)
7575

76-
7776
let comparable cmp =
7877
comparableU (fun[@bs] a b -> cmp a b)
79-
78+
8079
module type Hashable = sig
8180
type identity
8281
type t
@@ -86,9 +85,9 @@ end
8685

8786
type ('key, 'id) hashable = (module Hashable with type t = 'key and type identity = 'id)
8887

89-
module MakeHashable (M : sig
88+
module MakeHashableU (M : sig
9089
type t
91-
val hash : t -> int [@bs]
90+
val hash : t -> int [@bs]
9291
val eq : t -> t -> bool [@bs]
9392
end) =
9493
struct
@@ -97,9 +96,21 @@ struct
9796
let hash = M.hash
9897
let eq = M.eq
9998
end
100-
99+
100+
module MakeHashable (M : sig
101+
type t
102+
val hash : t -> int
103+
val eq : t -> t -> bool
104+
end) =
105+
struct
106+
type identity
107+
type t = M.t
108+
let hash = (fun[@bs] a -> M.hash a)
109+
let eq = (fun[@bs] a b -> M.eq a b)
110+
end
111+
101112
let hashableU (type key) ~hash ~eq =
102-
let module N = MakeHashable(struct
113+
let module N = MakeHashableU(struct
103114
type t = key
104115
let hash = hash
105116
let eq = eq

jscomp/others/belt_Id.mli

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,21 @@
3535

3636

3737

38-
type ('a, 'id) hash = 'a -> int [@bs]
38+
type ('a, 'id) hash
3939
(** [('a, 'id) hash]
4040
4141
Its runtime represenation is a [hash] function, but signed with a
4242
type parameter, so that different hash functions type mismatch
4343
*)
4444

45-
type ('a, 'id) eq = 'a -> 'a -> bool [@bs]
45+
type ('a, 'id) eq
4646
(** [('a, 'id) eq]
4747
4848
Its runtime represenation is an [eq] function, but signed with a
4949
type parameter, so that different hash functions type mismatch
5050
*)
5151

52-
type ('a, 'id) cmp = 'a -> 'a -> int [@bs]
52+
type ('a, 'id) cmp
5353
(** [('a,'id) cmp]
5454
5555
Its runtime representation is a [cmp] function, but signed with a
@@ -78,31 +78,25 @@ module MakeComparableU :
7878
functor (M : sig
7979
type t
8080
val cmp : t -> t -> int [@bs]
81-
end) ->
82-
sig
83-
type identity
84-
type t = M.t
85-
val cmp : M.t -> M.t -> int [@bs]
86-
end
81+
end) ->
82+
Comparable with type t = M.t
8783

8884
module MakeComparable :
8985
functor (M : sig
9086
type t
9187
val cmp : t -> t -> int
9288
end) ->
93-
sig
94-
type identity
95-
type t = M.t
96-
val cmp : M.t -> M.t -> int [@bs]
97-
end
89+
Comparable with type t = M.t
9890

9991
val comparableU:
10092
('a -> 'a -> int [@bs]) ->
10193
(module Comparable with type t = 'a)
94+
[@@ocaml.deprecated "Use the MakeComparableU functor API instead"]
10295

10396
val comparable:
10497
('a -> 'a -> int) ->
10598
(module Comparable with type t = 'a)
99+
[@@ocaml.deprecated "Use the MakeComparable functor API instead"]
106100

107101
module type Hashable = sig
108102
type identity
@@ -124,17 +118,33 @@ type ('key, 'id) hashable =
124118
mismatch if they use different comparison function
125119
*)
126120

127-
121+
module MakeHashableU :
122+
functor (M : sig
123+
type t
124+
val hash : t -> int [@bs]
125+
val eq : t -> t -> bool [@bs]
126+
end) ->
127+
Hashable with type t = M.t
128+
129+
module MakeHashable :
130+
functor (M : sig
131+
type t
132+
val hash : t -> int
133+
val eq : t -> t -> bool
134+
end) ->
135+
Hashable with type t = M.t
128136

129137
val hashableU :
130138
hash:('a -> int [@bs]) ->
131139
eq:('a -> 'a -> bool [@bs]) ->
132140
(module Hashable with type t = 'a)
133-
141+
[@@ocaml.deprecated "Use the MakeHashableU functor API instead"]
142+
134143
val hashable :
135144
hash:('a -> int) ->
136145
eq:('a -> 'a -> bool ) ->
137146
(module Hashable with type t = 'a)
147+
[@@ocaml.deprecated "Use the MakeHashable functor API instead"]
138148

139149

140150

lib/js/belt_Id.js

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,48 @@
22

33
var Curry = require("./curry.js");
44

5+
function MakeComparableU(M) {
6+
var cmp = M[/* cmp */0];
7+
return /* module */[/* cmp */cmp];
8+
}
9+
10+
function MakeComparable(M) {
11+
var cmp = function (a, b) {
12+
return Curry._2(M[/* cmp */0], a, b);
13+
};
14+
return /* module */[/* cmp */cmp];
15+
}
16+
517
function comparableU(cmp) {
618
return /* module */[/* cmp */cmp];
719
}
820

921
function comparable(cmp) {
10-
return comparableU(Curry.__2(cmp));
22+
var M = /* module */[/* cmp */Curry.__2(cmp)];
23+
var cmp$1 = M[/* cmp */0];
24+
return /* module */[/* cmp */cmp$1];
25+
}
26+
27+
function MakeHashableU(M) {
28+
var hash = M[/* hash */0];
29+
var eq = M[/* eq */1];
30+
return /* module */[
31+
/* hash */hash,
32+
/* eq */eq
33+
];
34+
}
35+
36+
function MakeHashable(M) {
37+
var hash = function (a) {
38+
return Curry._1(M[/* hash */0], a);
39+
};
40+
var eq = function (a, b) {
41+
return Curry._2(M[/* eq */1], a, b);
42+
};
43+
return /* module */[
44+
/* hash */hash,
45+
/* eq */eq
46+
];
1147
}
1248

1349
function hashableU(hash, eq) {
@@ -18,11 +54,22 @@ function hashableU(hash, eq) {
1854
}
1955

2056
function hashable(hash, eq) {
21-
return hashableU(Curry.__1(hash), Curry.__2(eq));
57+
var M_000 = Curry.__1(hash);
58+
var M_001 = Curry.__2(eq);
59+
var hash$1 = M_000;
60+
var eq$1 = M_001;
61+
return /* module */[
62+
/* hash */hash$1,
63+
/* eq */eq$1
64+
];
2265
}
2366

67+
exports.MakeComparableU = MakeComparableU;
68+
exports.MakeComparable = MakeComparable;
2469
exports.comparableU = comparableU;
2570
exports.comparable = comparable;
71+
exports.MakeHashableU = MakeHashableU;
72+
exports.MakeHashable = MakeHashable;
2673
exports.hashableU = hashableU;
2774
exports.hashable = hashable;
2875
/* No side effect */

0 commit comments

Comments
 (0)