Skip to content

Commit b90ad1e

Browse files
authored
Belt: clean up uncurried handling; deprecate xxxU functions (rescript-lang#6941)
* Belt: clean up uncurried handling; deprecate xxxU functions * CHANGELOG
1 parent 5a32e69 commit b90ad1e

File tree

165 files changed

+2382
-4042
lines changed

Some content is hidden

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

165 files changed

+2382
-4042
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
- Remove internal option `use-stdlib` from build schema. https://github.com/rescript-lang/rescript-compiler/pull/6778
101101
- Fix `Js.Types.JSBigInt` payload to use native `bigint` type. https://github.com/rescript-lang/rescript-compiler/pull/6911
102102
- Deprecate `%external` extension, which has never been officially introduced. https://github.com/rescript-lang/rescript-compiler/pull/6906
103+
- Deprecate `xxxU` functions in Belt. https://github.com/rescript-lang/rescript-compiler/pull/6941
103104

104105
# 11.1.3
105106

Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

22
We've found a bug for you!
3-
/.../fixtures/arity_mismatch3.res:1:21-33
3+
/.../fixtures/arity_mismatch3.res:1:20-30
44

5-
1 │ Belt.Array.mapU([], (. a, b) => 1)
5+
1 │ Belt.Array.map([], (a, b) => 1)
66
2 │
77

88
This function expected 1 argument, but got 2
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Belt.Array.mapU([], (. a, b) => 1)
1+
Belt.Array.map([], (a, b) => 1)

jscomp/others/belt.res

-30
Original file line numberDiff line numberDiff line change
@@ -77,36 +77,6 @@ let greaterThan2UniqueAndSorted =
7777
Js.log2("result", greaterThan2UniqueAndSorted)
7878
```
7979
80-
## Curried vs. Uncurried Callbacks
81-
82-
For functions taking a callback parameter, there are usually two versions
83-
available:
84-
85-
- curried (no suffix)
86-
- uncurried (suffixed with `U`)
87-
88-
E.g.:
89-
90-
## Examples
91-
92-
```rescript
93-
let forEach: (t<'a>, 'a => unit) => unit
94-
95-
let forEachU: (t<'a>, (. 'a) => unit) => unit
96-
```
97-
98-
The uncurried version will be faster in some cases, but for simplicity we recommend to stick with the curried version unless you need the extra performance.
99-
100-
The two versions can be invoked as follows:
101-
102-
## Examples
103-
104-
```rescript
105-
["a", "b", "c"]->Belt.Array.forEach(x => Js.log(x))
106-
107-
["a", "b", "c"]->Belt.Array.forEachU((. x) => Js.log(x))
108-
```
109-
11080
## Specialized Collections
11181
11282
For collections types like set or map, Belt provides both a generic module as well as specialized, more efficient implementations for string and int keys.

jscomp/others/belt_Array.res

+53-78
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ let make = (l, f) =>
116116

117117
/* See #6575. We could also check for maximum array size, but this depends
118118
on whether we create a float array or a regular one... */
119-
let makeByU = (l, f) =>
119+
let makeBy = (l, f) =>
120120
if l <= 0 {
121121
[]
122122
} else {
@@ -127,16 +127,12 @@ let makeByU = (l, f) =>
127127
res
128128
}
129129

130-
let makeBy = (l, f) => makeByU(l, a => f(a))
131-
132-
let makeByAndShuffleU = (l, f) => {
133-
let u = makeByU(l, f)
130+
let makeByAndShuffle = (l, f) => {
131+
let u = makeBy(l, f)
134132
shuffleInPlace(u)
135133
u
136134
}
137135

138-
let makeByAndShuffle = (l, f) => makeByAndShuffleU(l, a => f(a))
139-
140136
let range = (start, finish) => {
141137
let cut = finish - start
142138
if cut < 0 {
@@ -176,7 +172,7 @@ let zip = (xs, ys) => {
176172
s
177173
}
178174

179-
let zipByU = (xs, ys, f) => {
175+
let zipBy = (xs, ys, f) => {
180176
let (lenx, leny) = (length(xs), length(ys))
181177
let len = Pervasives.min(lenx, leny)
182178
let s = makeUninitializedUnsafe(len)
@@ -186,8 +182,6 @@ let zipByU = (xs, ys, f) => {
186182
s
187183
}
188184

189-
let zipBy = (xs, ys, f) => zipByU(xs, ys, (a, b) => f(a, b))
190-
191185
let concat = (a1, a2) => {
192186
let l1 = length(a1)
193187
let l2 = length(a2)
@@ -325,14 +319,12 @@ let blit = (~src as a1, ~srcOffset as ofs1, ~dst as a2, ~dstOffset as ofs2, ~len
325319
}
326320
}
327321

328-
let forEachU = (a, f) =>
322+
let forEach = (a, f) =>
329323
for i in 0 to length(a) - 1 {
330324
f(getUnsafe(a, i))
331325
}
332326

333-
let forEach = (a, f) => forEachU(a, a => f(a))
334-
335-
let mapU = (a, f) => {
327+
let map = (a, f) => {
336328
let l = length(a)
337329
let r = makeUninitializedUnsafe(l)
338330
for i in 0 to l - 1 {
@@ -341,13 +333,9 @@ let mapU = (a, f) => {
341333
r
342334
}
343335

344-
let map = (a, f) => mapU(a, a => f(a))
336+
let flatMap = (a, f) => concatMany(map(a, f))
345337

346-
let flatMapU = (a, f) => concatMany(mapU(a, f))
347-
348-
let flatMap = (a, f) => flatMapU(a, a => f(a))
349-
350-
let getByU = (a, p) => {
338+
let getBy = (a, p) => {
351339
let l = length(a)
352340
let i = ref(0)
353341
let r = ref(None)
@@ -361,9 +349,7 @@ let getByU = (a, p) => {
361349
r.contents
362350
}
363351

364-
let getBy = (a, p) => getByU(a, a => p(a))
365-
366-
let getIndexByU = (a, p) => {
352+
let getIndexBy = (a, p) => {
367353
let l = length(a)
368354
let i = ref(0)
369355
let r = ref(None)
@@ -377,9 +363,7 @@ let getIndexByU = (a, p) => {
377363
r.contents
378364
}
379365

380-
let getIndexBy = (a, p) => getIndexByU(a, a => p(a))
381-
382-
let keepU = (a, f) => {
366+
let keep = (a, f) => {
383367
let l = length(a)
384368
let r = makeUninitializedUnsafe(l)
385369
let j = ref(0)
@@ -394,9 +378,7 @@ let keepU = (a, f) => {
394378
r
395379
}
396380

397-
let keep = (a, f) => keepU(a, a => f(a))
398-
399-
let keepWithIndexU = (a, f) => {
381+
let keepWithIndex = (a, f) => {
400382
let l = length(a)
401383
let r = makeUninitializedUnsafe(l)
402384
let j = ref(0)
@@ -411,9 +393,7 @@ let keepWithIndexU = (a, f) => {
411393
r
412394
}
413395

414-
let keepWithIndex = (a, f) => keepWithIndexU(a, (a, i) => f(a, i))
415-
416-
let keepMapU = (a, f) => {
396+
let keepMap = (a, f) => {
417397
let l = length(a)
418398
let r = makeUninitializedUnsafe(l)
419399
let j = ref(0)
@@ -430,16 +410,12 @@ let keepMapU = (a, f) => {
430410
r
431411
}
432412

433-
let keepMap = (a, f) => keepMapU(a, a => f(a))
434-
435-
let forEachWithIndexU = (a, f) =>
413+
let forEachWithIndex = (a, f) =>
436414
for i in 0 to length(a) - 1 {
437415
f(i, getUnsafe(a, i))
438416
}
439417

440-
let forEachWithIndex = (a, f) => forEachWithIndexU(a, (a, b) => f(a, b))
441-
442-
let mapWithIndexU = (a, f) => {
418+
let mapWithIndex = (a, f) => {
443419
let l = length(a)
444420
let r = makeUninitializedUnsafe(l)
445421
for i in 0 to l - 1 {
@@ -448,29 +424,23 @@ let mapWithIndexU = (a, f) => {
448424
r
449425
}
450426

451-
let mapWithIndex = (a, f) => mapWithIndexU(a, (a, b) => f(a, b))
452-
453-
let reduceU = (a, x, f) => {
427+
let reduce = (a, x, f) => {
454428
let r = ref(x)
455429
for i in 0 to length(a) - 1 {
456430
r.contents = f(r.contents, getUnsafe(a, i))
457431
}
458432
r.contents
459433
}
460434

461-
let reduce = (a, x, f) => reduceU(a, x, (a, b) => f(a, b))
462-
463-
let reduceReverseU = (a, x, f) => {
435+
let reduceReverse = (a, x, f) => {
464436
let r = ref(x)
465437
for i in length(a) - 1 downto 0 {
466438
r.contents = f(r.contents, getUnsafe(a, i))
467439
}
468440
r.contents
469441
}
470442

471-
let reduceReverse = (a, x, f) => reduceReverseU(a, x, (a, b) => f(a, b))
472-
473-
let reduceReverse2U = (a, b, x, f) => {
443+
let reduceReverse2 = (a, b, x, f) => {
474444
let r = ref(x)
475445
let len = Pervasives.min(length(a), length(b))
476446
for i in len - 1 downto 0 {
@@ -479,18 +449,14 @@ let reduceReverse2U = (a, b, x, f) => {
479449
r.contents
480450
}
481451

482-
let reduceReverse2 = (a, b, x, f) => reduceReverse2U(a, b, x, (a, b, c) => f(a, b, c))
483-
484-
let reduceWithIndexU = (a, x, f) => {
452+
let reduceWithIndex = (a, x, f) => {
485453
let r = ref(x)
486454
for i in 0 to length(a) - 1 {
487455
r.contents = f(r.contents, getUnsafe(a, i), i)
488456
}
489457
r.contents
490458
}
491459

492-
let reduceWithIndex = (a, x, f) => reduceWithIndexU(a, x, (a, b, c) => f(a, b, c))
493-
494460
let rec everyAux = (arr, i, b, len) =>
495461
if i == len {
496462
true
@@ -509,20 +475,16 @@ let rec someAux = (arr, i, b, len) =>
509475
someAux(arr, i + 1, b, len)
510476
}
511477

512-
let everyU = (arr, b) => {
478+
let every = (arr, b) => {
513479
let len = length(arr)
514480
everyAux(arr, 0, b, len)
515481
}
516482

517-
let every = (arr, f) => everyU(arr, b => f(b))
518-
519-
let someU = (arr, b) => {
483+
let some = (arr, b) => {
520484
let len = length(arr)
521485
someAux(arr, 0, b, len)
522486
}
523487

524-
let some = (arr, f) => someU(arr, b => f(b))
525-
526488
let rec everyAux2 = (arr1, arr2, i, b, len) =>
527489
if i == len {
528490
true
@@ -541,15 +503,11 @@ let rec someAux2 = (arr1, arr2, i, b, len) =>
541503
someAux2(arr1, arr2, i + 1, b, len)
542504
}
543505

544-
let every2U = (a, b, p) => everyAux2(a, b, 0, p, Pervasives.min(length(a), length(b)))
506+
let every2 = (a, b, p) => everyAux2(a, b, 0, p, Pervasives.min(length(a), length(b)))
545507

546-
let every2 = (a, b, p) => every2U(a, b, (a, b) => p(a, b))
508+
let some2 = (a, b, p) => someAux2(a, b, 0, p, Pervasives.min(length(a), length(b)))
547509

548-
let some2U = (a, b, p) => someAux2(a, b, 0, p, Pervasives.min(length(a), length(b)))
549-
550-
let some2 = (a, b, p) => some2U(a, b, (a, b) => p(a, b))
551-
552-
let eqU = (a, b, p) => {
510+
let eq = (a, b, p) => {
553511
let lena = length(a)
554512
let lenb = length(b)
555513
if lena == lenb {
@@ -559,8 +517,6 @@ let eqU = (a, b, p) => {
559517
}
560518
}
561519

562-
let eq = (a, b, p) => eqU(a, b, (a, b) => p(a, b))
563-
564520
let rec everyCmpAux2 = (arr1, arr2, i, b, len) =>
565521
if i == len {
566522
0
@@ -573,7 +529,7 @@ let rec everyCmpAux2 = (arr1, arr2, i, b, len) =>
573529
}
574530
}
575531

576-
let cmpU = (a, b, p) => {
532+
let cmp = (a, b, p) => {
577533
let lena = length(a)
578534
let lenb = length(b)
579535
if lena > lenb {
@@ -585,9 +541,7 @@ let cmpU = (a, b, p) => {
585541
}
586542
}
587543

588-
let cmp = (a, b, p) => cmpU(a, b, (a, b) => p(a, b))
589-
590-
let partitionU = (a, f) => {
544+
let partition = (a, f) => {
591545
let l = length(a)
592546
let i = ref(0)
593547
let j = ref(0)
@@ -608,8 +562,6 @@ let partitionU = (a, f) => {
608562
(a1, a2)
609563
}
610564

611-
let partition = (a, f) => partitionU(a, x => f(x))
612-
613565
let unzip = a => {
614566
let l = length(a)
615567
let a1 = makeUninitializedUnsafe(l)
@@ -622,7 +574,7 @@ let unzip = a => {
622574
(a1, a2)
623575
}
624576

625-
let joinWithU = (a, sep, toString) =>
577+
let joinWith = (a, sep, toString) =>
626578
switch length(a) {
627579
| 0 => ""
628580
| l =>
@@ -637,16 +589,39 @@ let joinWithU = (a, sep, toString) =>
637589
aux(0, "")
638590
}
639591

640-
let joinWith = (a, sep, toString) => joinWithU(a, sep, x => toString(x))
641-
642-
let initU = (n, f) => {
592+
let init = (n, f) => {
643593
let v = makeUninitializedUnsafe(n)
644594
for i in 0 to n - 1 {
645595
setUnsafe(v, i, f(i))
646596
}
647597
v
648598
}
649599

650-
let init = (n, f) => initU(n, i => f(i))
600+
let cmpU = cmp
601+
let eqU = eq
602+
let every2U = every2
603+
let everyU = every
604+
let flatMapU = flatMap
605+
let forEachU = forEach
606+
let forEachWithIndexU = forEachWithIndex
607+
let getByU = getBy
608+
let getIndexByU = getIndexBy
609+
let initU = init
610+
let joinWithU = joinWith
611+
let keepMapU = keepMap
612+
let keepU = keep
613+
let keepWithIndexU = keepWithIndex
614+
let makeByAndShuffleU = makeByAndShuffle
615+
let makeByU = makeBy
616+
let mapU = map
617+
let mapWithIndexU = mapWithIndex
618+
let partitionU = partition
619+
let reduceReverse2U = reduceReverse2
620+
let reduceReverseU = reduceReverse
621+
let reduceU = reduce
622+
let reduceWithIndexU = reduceWithIndex
623+
let some2U = some2
624+
let someU = some
625+
let zipByU = zipBy
651626

652627
@send external push: (t<'a>, 'a) => unit = "push"

0 commit comments

Comments
 (0)