Skip to content

Commit d1459ba

Browse files
committed
add curried counter-part
1 parent e9034b4 commit d1459ba

File tree

87 files changed

+1339
-722
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+1339
-722
lines changed

jscomp/others/bs_Array.ml

+41-17
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ let make l f =
9090

9191
(* See #6575. We could also check for maximum array size, but this depends
9292
on whether we create a float array or a regular one... *)
93-
let makeBy l f =
93+
let makeByU l f =
9494
if l <= 0 then [||]
9595
else
9696
let res = makeUninitializedUnsafe l in
@@ -99,11 +99,14 @@ let makeBy l f =
9999
done;
100100
res
101101

102-
let makeByAndShuffle l f =
103-
let u = makeBy l f in
102+
let makeBy l f = makeByU l (fun[@bs] a -> f a)
103+
104+
let makeByAndShuffleU l f =
105+
let u = makeByU l f in
104106
shuffleInPlace u ;
105107
u
106108

109+
let makeByAndShuffle l f = makeByAndShuffleU l (fun[@bs] a -> f a)
107110

108111

109112

@@ -116,7 +119,7 @@ let zip xs ys =
116119
done ;
117120
s
118121

119-
let zipBy xs ys f =
122+
let zipByU xs ys f =
120123
let lenx, leny = length xs, length ys in
121124
let len = Pervasives.min lenx leny in
122125
let s = makeUninitializedUnsafe len in
@@ -125,6 +128,8 @@ let zipBy xs ys f =
125128
done ;
126129
s
127130

131+
let zipBy xs ys f = zipByU xs ys (fun [@bs] a b -> f a b)
132+
128133
let concat a1 a2 =
129134
let l1 = length a1 in
130135
let l2 = length a2 in
@@ -216,19 +221,22 @@ let blit ~src:a1 ~srcOffset:ofs1 ~dst:a2 ~dstOffset:ofs2 ~len =
216221
setUnsafe a2 (j + srcofs2) (getUnsafe a1 (j + srcofs1))
217222
done
218223

219-
let forEach a f =
224+
let forEachU a f =
220225
for i = 0 to length a - 1 do f(getUnsafe a i) [@bs] done
221226

222-
let map a f =
227+
let forEach a f = forEachU a (fun[@bs] a -> f a)
228+
229+
let mapU a f =
223230
let l = length a in
224231
let r = makeUninitializedUnsafe l in
225232
for i = 0 to l - 1 do
226233
setUnsafe r i (f(getUnsafe a i) [@bs])
227234
done;
228235
r
236+
237+
let map a f = mapU a (fun[@bs] a -> f a)
229238

230-
231-
let keep a f =
239+
let keepU a f =
232240
let l = length a in
233241
let r = makeUninitializedUnsafe l in
234242
let j = ref 0 in
@@ -243,7 +251,9 @@ let keep a f =
243251
truncateToLengthUnsafe r !j;
244252
r
245253

246-
let keepMap a f =
254+
let keep a f = keepU a (fun [@bs] a -> f a)
255+
256+
let keepMapU a f =
247257
let l = length a in
248258
let r = makeUninitializedUnsafe l in
249259
let j = ref 0 in
@@ -260,55 +270,69 @@ let keepMap a f =
260270
truncateToLengthUnsafe r !j;
261271
r
262272

263-
let forEachWithIndex a f=
273+
let keepMap a f = keepMapU a (fun[@bs] a -> f a)
274+
275+
let forEachWithIndexU a f=
264276
for i = 0 to length a - 1 do f i (getUnsafe a i) [@bs] done
265277

266-
let mapWithIndex a f =
278+
let forEachWithIndex a f = forEachWithIndexU a (fun[@bs] a b -> f a b)
279+
280+
let mapWithIndexU a f =
267281
let l = length a in
268282
let r = makeUninitializedUnsafe l in
269283
for i = 0 to l - 1 do
270284
setUnsafe r i (f i (getUnsafe a i) [@bs])
271285
done;
272286
r
273287

274-
let reduce a x f =
288+
let mapWithIndex a f = mapWithIndexU a (fun[@bs] a b -> f a b)
289+
290+
let reduceU a x f =
275291
let r = ref x in
276292
for i = 0 to length a - 1 do
277293
r := f !r (getUnsafe a i) [@bs]
278294
done;
279295
!r
280296

281-
let reduceReverse a x f =
297+
let reduce a x f = reduceU a x (fun[@bs] a b -> f a b)
298+
299+
let reduceReverseU a x f =
282300
let r = ref x in
283301
for i = length a - 1 downto 0 do
284302
r := f !r (getUnsafe a i) [@bs]
285303
done;
286304
!r
287305

306+
let reduceReverse a x f = reduceReverseU a x (fun[@bs] a b -> f a b)
288307

289308
let rec everyAux arr i b len =
290309
if i = len then true
291310
else if b (getUnsafe arr i) [@bs] then
292311
everyAux arr (i + 1) b len
293312
else false
294313

295-
let every arr b =
314+
let everyU arr b =
296315
let len = length arr in
297316
everyAux arr 0 b len
298317

318+
let every arr f = everyU arr (fun[@bs] b -> f b)
319+
299320
let rec everyAux2 arr1 arr2 i b len =
300321
if i = len then true
301322
else if b (getUnsafe arr1 i) (getUnsafe arr2 i) [@bs] then
302323
everyAux2 arr1 arr2 (i + 1) b len
303324
else false
304325

305-
let every2 a b p =
326+
let every2U a b p =
306327
let lena = length a in
307328
let lenb = length b in
308329
if lena <> lenb then false
309330
else
310331
everyAux2 a b 0 p lena
311332

333+
let every2 a b p = every2U a b (fun[@bs] a b -> p a b)
334+
335+
let eqU = every2U
312336
let eq = every2
313337

314338
let rec everyCmpAux2 arr1 arr2 i b len =
@@ -319,14 +343,14 @@ let rec everyCmpAux2 arr1 arr2 i b len =
319343
everyCmpAux2 arr1 arr2 (i + 1) b len
320344
else c
321345

322-
let cmp a b p =
346+
let cmpU a b p =
323347
let lena = length a in
324348
let lenb = length b in
325349
if lena > lenb then 1
326350
else if lena < lenb then -1
327351
else everyCmpAux2 a b 0 p lena
328352

329-
353+
let cmp a b p = cmpU a b (fun[@bs] a b -> p a b)
330354

331355

332356

jscomp/others/bs_Array.mli

+32-17
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,16 @@ val make: int -> 'a -> 'a array
7474
return an array of size [n] with value [e]
7575
*)
7676

77-
val makeBy: int -> (int -> 'a [@bs]) -> 'a array
77+
val makeByU: int -> (int -> 'a [@bs]) -> 'a array
78+
val makeBy: int -> (int -> 'a ) -> 'a array
7879
(** [makeBy n f]
7980
8081
return an empty array when [n] is negative
8182
return an array of size [n] populated by [f i] start from [0] to [n - 1]
8283
*)
8384

84-
val makeByAndShuffle: int -> (int -> 'a [@bs]) -> 'a array
85+
val makeByAndShuffleU: int -> (int -> 'a [@bs]) -> 'a array
86+
val makeByAndShuffle: int -> (int -> 'a ) -> 'a array
8587
(** [makeByAndShuffle n f] is semantically equivalent to [makeBy n f]
8688
and return the shuffled array *)
8789

@@ -93,7 +95,8 @@ val zip: 'a array -> 'b array -> ('a * 'b) array
9395
*)
9496

9597

96-
val zipBy: 'a array -> 'b array -> ('a -> 'b -> 'c [@bs]) -> 'c array
98+
val zipByU: 'a array -> 'b array -> ('a -> 'b -> 'c [@bs]) -> 'c array
99+
val zipBy: 'a array -> 'b array -> ('a -> 'b -> 'c ) -> 'c array
97100
(**
98101
[zipBy a b f]
99102
@@ -166,39 +169,51 @@ val blitUnsafe:
166169
src:'a array -> srcOffset:int -> dst:'a array -> dstOffset:int -> len:int -> unit
167170

168171

169-
val forEach: 'a array -> ('a -> unit [@bs]) -> unit
172+
val forEachU: 'a array -> ('a -> unit [@bs]) -> unit
173+
val forEach: 'a array -> ('a -> unit ) -> unit
170174

171-
val map: 'a array -> ('a -> 'b [@bs]) -> 'b array
175+
val mapU: 'a array -> ('a -> 'b [@bs]) -> 'b array
176+
val map: 'a array -> ('a -> 'b ) -> 'b array
172177

173-
val keep: 'a array -> ('a -> bool [@bs]) -> 'a array
178+
val keepU: 'a array -> ('a -> bool [@bs]) -> 'a array
179+
val keep: 'a array -> ('a -> bool ) -> 'a array
174180

175-
val keepMap: 'a array -> ('a -> 'b option [@bs]) -> 'b array
176-
177-
val forEachWithIndex: 'a array -> (int -> 'a -> unit [@bs]) -> unit
178-
179-
val mapWithIndex: 'a array -> (int -> 'a -> 'b [@bs]) -> 'b array
180181

181-
val reduce: 'b array -> 'a -> ('a -> 'b -> 'a [@bs]) ->'a
182+
val keepMapU: 'a array -> ('a -> 'b option [@bs]) -> 'b array
183+
val keepMap: 'a array -> ('a -> 'b option) -> 'b array
184+
185+
val forEachWithIndexU: 'a array -> (int -> 'a -> unit [@bs]) -> unit
186+
val forEachWithIndex: 'a array -> (int -> 'a -> unit ) -> unit
182187

183-
val reduceReverse: 'b array -> 'a -> ('a -> 'b -> 'a [@bs]) -> 'a
188+
val mapWithIndexU: 'a array -> (int -> 'a -> 'b [@bs]) -> 'b array
189+
val mapWithIndex: 'a array -> (int -> 'a -> 'b ) -> 'b array
184190

185-
val every: 'a array -> ('a -> bool [@bs]) -> bool
191+
val reduceU: 'b array -> 'a -> ('a -> 'b -> 'a [@bs]) ->'a
192+
val reduce: 'b array -> 'a -> ('a -> 'b -> 'a ) ->'a
186193

194+
val reduceReverseU: 'b array -> 'a -> ('a -> 'b -> 'a [@bs]) -> 'a
195+
val reduceReverse: 'b array -> 'a -> ('a -> 'b -> 'a ) -> 'a
196+
197+
val everyU: 'a array -> ('a -> bool [@bs]) -> bool
198+
val every: 'a array -> ('a -> bool ) -> bool
187199

188-
val every2: 'a array -> 'b array -> ('a -> 'b -> bool [@bs]) -> bool
200+
val every2U: 'a array -> 'b array -> ('a -> 'b -> bool [@bs]) -> bool
201+
val every2: 'a array -> 'b array -> ('a -> 'b -> bool ) -> bool
189202
(** [every2 a b p]
190203
- return false when [length a <> length b]
191204
- return true when every pair is true [f ai bi]
192205
*)
193206

194-
val cmp: 'a array -> 'a array -> ('a -> 'a -> int [@bs]) -> int
207+
val cmpU: 'a array -> 'a array -> ('a -> 'a -> int [@bs]) -> int
208+
val cmp: 'a array -> 'a array -> ('a -> 'a -> int ) -> int
195209
(** [cmp a b]
196210
197211
if [length a <> length b] compared by length
198212
otherwise compare one by one [f ai bi]
199213
*)
200214

201-
val eq: 'a array -> 'a array -> ('a -> 'a -> bool [@bs]) -> bool
215+
val eqU: 'a array -> 'a array -> ('a -> 'a -> bool [@bs]) -> bool
216+
val eq: 'a array -> 'a array -> ('a -> 'a -> bool ) -> bool
202217
(** [eq a b]
203218
204219
- return false if length is not the same

jscomp/others/bs_Dict.ml

+12-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ module type Comparable = sig
3939
val cmp: (t, id) cmp
4040
end
4141

42-
type ('key, 'id) comparable = (module Comparable with type t = 'key and type id = 'id)
42+
type ('key, 'id) comparable =
43+
(module Comparable with type t = 'key and type id = 'id)
4344

4445

4546
module MakeComparable (M : sig
@@ -52,7 +53,7 @@ struct
5253
let cmp = M.cmp
5354
end
5455

55-
let comparable
56+
let comparableU
5657
(type key)
5758
~cmp
5859
=
@@ -63,6 +64,9 @@ let comparable
6364
(module N : Comparable with type t = key)
6465

6566

67+
let comparable ~cmp =
68+
comparableU ~cmp:(fun[@bs] a b -> cmp a b)
69+
6670
module type Hashable = sig
6771
type id
6872
type t
@@ -84,10 +88,15 @@ struct
8488
let eq = M.eq
8589
end
8690

87-
let hashable (type key) ~hash ~eq =
91+
let hashableU (type key) ~hash ~eq =
8892
let module N = MakeHashable(struct
8993
type t = key
9094
let hash = hash
9195
let eq = eq
9296
end) in
9397
(module N : Hashable with type t = key)
98+
99+
let hashable ~hash ~eq =
100+
hashableU
101+
~hash:(fun [@bs] a -> hash a)
102+
~eq:(fun [@bs] a b -> eq a b)

jscomp/others/bs_Dict.mli

+19-6
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ module type Comparable = sig
6262
val cmp: (t, id) cmp
6363
end
6464

65-
type ('key, 'id) comparable = (module Comparable with type t = 'key and type id = 'id)
65+
type ('key, 'id) comparable =
66+
(module Comparable with type t = 'key and type id = 'id)
6667
(** [('key, 'id) cmparable] is a module of functions, here it only includes [cmp].
6768
6869
Unlike normal functions, when created, it comes with a unique identity (guaranteed
@@ -74,19 +75,23 @@ type ('key, 'id) comparable = (module Comparable with type t = 'key and type id
7475
mismatch if they use different comparison function
7576
*)
7677

77-
val comparable:
78+
val comparableU:
7879
cmp:('a -> 'a -> int [@bs]) ->
7980
(module Comparable with type t = 'a)
8081

81-
82+
val comparable:
83+
cmp:('a -> 'a -> int) ->
84+
(module Comparable with type t = 'a)
85+
8286
module type Hashable = sig
8387
type id
8488
type t
8589
val hash: (t,id) hash
8690
val eq: (t,id) eq
8791
end
8892

89-
type ('key, 'id) hashable = (module Hashable with type t = 'key and type id = 'id)
93+
type ('key, 'id) hashable =
94+
(module Hashable with type t = 'key and type id = 'id)
9095
(** [('key, 'id) hashable] is a module of functions, here it only includes [hash], [eq].
9196
9297
Unlike normal functions, when created, it comes with a unique identity (guaranteed
@@ -100,9 +105,17 @@ type ('key, 'id) hashable = (module Hashable with type t = 'key and type id = 'i
100105

101106

102107

103-
val hashable:
108+
val hashableU:
104109
hash:('a -> int [@bs]) ->
105-
eq:('a -> 'a -> bool [@bs]) -> (module Hashable with type t = 'a)
110+
eq:('a -> 'a -> bool [@bs]) ->
111+
(module Hashable with type t = 'a)
112+
113+
val hashable:
114+
hash:('a -> int ) ->
115+
eq:('a -> 'a -> bool ) ->
116+
(module Hashable with type t = 'a)
117+
118+
106119

107120
(**/**)
108121
external getHashInternal : ('a,'id) hash -> ('a -> int [@bs]) = "%identity"

0 commit comments

Comments
 (0)