Skip to content

Commit d5cac82

Browse files
author
Cristiano Calcagno
committed
[Belt] Add Array.unzip.
Noticed Array.unzip missing when converting some code from using lists to using arrays.
1 parent e867911 commit d5cac82

File tree

5 files changed

+100
-38
lines changed

5 files changed

+100
-38
lines changed

jscomp/others/belt_Array.ml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,5 +408,13 @@ let cmpU a b p =
408408

409409
let cmp a b p = cmpU a b (fun[@bs] a b -> p a b)
410410

411-
412-
411+
let unzip a =
412+
let l = length a in
413+
let a1 = makeUninitializedUnsafe l in
414+
let a2 = makeUninitializedUnsafe l in
415+
for i = 0 to l - 1 do
416+
let (v1, v2) = getUnsafe a i in
417+
setUnsafe a1 i v1;
418+
setUnsafe a2 i v2
419+
done;
420+
(a1, a2)

jscomp/others/belt_Array.mli

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,15 @@ val zip: 'a array -> 'b array -> ('a * 'b) array
204204
]}
205205
*)
206206

207+
val unzip: ('a * 'b) array -> 'a array * 'b array
208+
(** [unzip a] takes an array of pairs and creates a pair of arrays. The first array contains all the first items of the pairs; the second array contains all the second items.
209+
210+
@example {[
211+
unzip [|(1,2) ; (3,4)|] = ([|1;3|], [|2;4|]);;
212+
unzip [|(1,2) ; (3,4) ; (5,6) ; (7,8)|] = ([|1;3;5;7|], [|2;4;6;8|]);;
213+
]}
214+
*)
215+
207216
val concat: 'a array -> 'a array -> 'a array
208217
(** [concat xs ys]
209218

jscomp/test/bs_array_test.js

Lines changed: 61 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,32 @@ eq("File \"bs_array_test.ml\", line 237, characters 5-12", Belt_Array.zipBy(/* a
10511051
return -x | 0;
10521052
})));
10531053

1054+
eq("File \"bs_array_test.ml\", line 238, characters 5-12", Belt_Array.unzip(/* array */[
1055+
/* tuple */[
1056+
1,
1057+
2
1058+
],
1059+
/* tuple */[
1060+
2,
1061+
3
1062+
],
1063+
/* tuple */[
1064+
3,
1065+
4
1066+
]
1067+
]), /* tuple */[
1068+
/* array */[
1069+
1,
1070+
2,
1071+
3
1072+
],
1073+
/* array */[
1074+
2,
1075+
3,
1076+
4
1077+
]
1078+
]);
1079+
10541080
function sumUsingForEach(xs) {
10551081
var v = [0];
10561082
Belt_Array.forEach(xs, (function (x) {
@@ -1060,15 +1086,15 @@ function sumUsingForEach(xs) {
10601086
return v[0];
10611087
}
10621088

1063-
eq("File \"bs_array_test.ml\", line 245, characters 5-12", sumUsingForEach(/* array */[
1089+
eq("File \"bs_array_test.ml\", line 248, characters 5-12", sumUsingForEach(/* array */[
10641090
0,
10651091
1,
10661092
2,
10671093
3,
10681094
4
10691095
]), 10);
10701096

1071-
b("File \"bs_array_test.ml\", line 246, characters 4-11", !Belt_Array.every(/* array */[
1097+
b("File \"bs_array_test.ml\", line 249, characters 4-11", !Belt_Array.every(/* array */[
10721098
0,
10731099
1,
10741100
2,
@@ -1078,7 +1104,7 @@ b("File \"bs_array_test.ml\", line 246, characters 4-11", !Belt_Array.every(/* a
10781104
return x > 2;
10791105
})));
10801106

1081-
b("File \"bs_array_test.ml\", line 247, characters 4-11", Belt_Array.some(/* array */[
1107+
b("File \"bs_array_test.ml\", line 250, characters 4-11", Belt_Array.some(/* array */[
10821108
1,
10831109
3,
10841110
7,
@@ -1087,22 +1113,22 @@ b("File \"bs_array_test.ml\", line 247, characters 4-11", Belt_Array.some(/* arr
10871113
return x % 2 === 0;
10881114
})));
10891115

1090-
b("File \"bs_array_test.ml\", line 248, characters 4-11", !Belt_Array.some(/* array */[
1116+
b("File \"bs_array_test.ml\", line 251, characters 4-11", !Belt_Array.some(/* array */[
10911117
1,
10921118
3,
10931119
7
10941120
], (function (x) {
10951121
return x % 2 === 0;
10961122
})));
10971123

1098-
b("File \"bs_array_test.ml\", line 249, characters 4-11", !Belt_Array.eq(/* array */[
1124+
b("File \"bs_array_test.ml\", line 252, characters 4-11", !Belt_Array.eq(/* array */[
10991125
0,
11001126
1
11011127
], /* array */[1], Caml_obj.caml_equal));
11021128

11031129
var c$1 = [0];
11041130

1105-
b("File \"bs_array_test.ml\", line 250, characters 4-11", (Belt_Array.forEachWithIndex(/* array */[
1131+
b("File \"bs_array_test.ml\", line 253, characters 4-11", (Belt_Array.forEachWithIndex(/* array */[
11061132
1,
11071133
1,
11081134
1
@@ -1113,25 +1139,25 @@ b("File \"bs_array_test.ml\", line 250, characters 4-11", (Belt_Array.forEachWit
11131139

11141140
function id$1(_, x) {
11151141
var u = x.slice(0);
1116-
return eq("File \"bs_array_test.ml\", line 260, characters 5-12", Belt_Array.reverse(x), (Belt_Array.reverseInPlace(u), u));
1142+
return eq("File \"bs_array_test.ml\", line 263, characters 5-12", Belt_Array.reverse(x), (Belt_Array.reverseInPlace(u), u));
11171143
}
11181144

1119-
id$1("File \"bs_array_test.ml\", line 265, characters 5-12", /* array */[]);
1145+
id$1("File \"bs_array_test.ml\", line 268, characters 5-12", /* array */[]);
11201146

1121-
id$1("File \"bs_array_test.ml\", line 266, characters 5-12", /* array */[1]);
1147+
id$1("File \"bs_array_test.ml\", line 269, characters 5-12", /* array */[1]);
11221148

1123-
id$1("File \"bs_array_test.ml\", line 267, characters 5-12", /* array */[
1149+
id$1("File \"bs_array_test.ml\", line 270, characters 5-12", /* array */[
11241150
1,
11251151
2
11261152
]);
11271153

1128-
id$1("File \"bs_array_test.ml\", line 268, characters 5-12", /* array */[
1154+
id$1("File \"bs_array_test.ml\", line 271, characters 5-12", /* array */[
11291155
1,
11301156
2,
11311157
3
11321158
]);
11331159

1134-
id$1("File \"bs_array_test.ml\", line 269, characters 5-12", /* array */[
1160+
id$1("File \"bs_array_test.ml\", line 272, characters 5-12", /* array */[
11351161
1,
11361162
2,
11371163
3,
@@ -1154,14 +1180,14 @@ function some2(xs, ys) {
11541180
});
11551181
}
11561182

1157-
eq("File \"bs_array_test.ml\", line 279, characters 5-12", every2(/* [] */0, /* :: */[
1183+
eq("File \"bs_array_test.ml\", line 282, characters 5-12", every2(/* [] */0, /* :: */[
11581184
1,
11591185
/* [] */0
11601186
])((function (x, y) {
11611187
return x > y;
11621188
})), true);
11631189

1164-
eq("File \"bs_array_test.ml\", line 280, characters 5-12", every2(/* :: */[
1190+
eq("File \"bs_array_test.ml\", line 283, characters 5-12", every2(/* :: */[
11651191
2,
11661192
/* :: */[
11671193
3,
@@ -1174,7 +1200,7 @@ eq("File \"bs_array_test.ml\", line 280, characters 5-12", every2(/* :: */[
11741200
return x > y;
11751201
})), true);
11761202

1177-
eq("File \"bs_array_test.ml\", line 281, characters 5-12", every2(/* :: */[
1203+
eq("File \"bs_array_test.ml\", line 284, characters 5-12", every2(/* :: */[
11781204
2,
11791205
/* [] */0
11801206
], /* :: */[
@@ -1184,7 +1210,7 @@ eq("File \"bs_array_test.ml\", line 281, characters 5-12", every2(/* :: */[
11841210
return x > y;
11851211
})), true);
11861212

1187-
eq("File \"bs_array_test.ml\", line 282, characters 5-12", every2(/* :: */[
1213+
eq("File \"bs_array_test.ml\", line 285, characters 5-12", every2(/* :: */[
11881214
2,
11891215
/* :: */[
11901216
3,
@@ -1200,7 +1226,7 @@ eq("File \"bs_array_test.ml\", line 282, characters 5-12", every2(/* :: */[
12001226
return x > y;
12011227
})), false);
12021228

1203-
eq("File \"bs_array_test.ml\", line 283, characters 5-12", every2(/* :: */[
1229+
eq("File \"bs_array_test.ml\", line 286, characters 5-12", every2(/* :: */[
12041230
2,
12051231
/* :: */[
12061232
3,
@@ -1216,14 +1242,14 @@ eq("File \"bs_array_test.ml\", line 283, characters 5-12", every2(/* :: */[
12161242
return x > y;
12171243
})), true);
12181244

1219-
eq("File \"bs_array_test.ml\", line 284, characters 5-12", some2(/* [] */0, /* :: */[
1245+
eq("File \"bs_array_test.ml\", line 287, characters 5-12", some2(/* [] */0, /* :: */[
12201246
1,
12211247
/* [] */0
12221248
])((function (x, y) {
12231249
return x > y;
12241250
})), false);
12251251

1226-
eq("File \"bs_array_test.ml\", line 285, characters 5-12", some2(/* :: */[
1252+
eq("File \"bs_array_test.ml\", line 288, characters 5-12", some2(/* :: */[
12271253
2,
12281254
/* :: */[
12291255
3,
@@ -1236,7 +1262,7 @@ eq("File \"bs_array_test.ml\", line 285, characters 5-12", some2(/* :: */[
12361262
return x > y;
12371263
})), true);
12381264

1239-
eq("File \"bs_array_test.ml\", line 286, characters 5-12", some2(/* :: */[
1265+
eq("File \"bs_array_test.ml\", line 289, characters 5-12", some2(/* :: */[
12401266
2,
12411267
/* :: */[
12421268
3,
@@ -1252,7 +1278,7 @@ eq("File \"bs_array_test.ml\", line 286, characters 5-12", some2(/* :: */[
12521278
return x > y;
12531279
})), true);
12541280

1255-
eq("File \"bs_array_test.ml\", line 287, characters 5-12", some2(/* :: */[
1281+
eq("File \"bs_array_test.ml\", line 290, characters 5-12", some2(/* :: */[
12561282
0,
12571283
/* :: */[
12581284
3,
@@ -1268,7 +1294,7 @@ eq("File \"bs_array_test.ml\", line 287, characters 5-12", some2(/* :: */[
12681294
return x > y;
12691295
})), false);
12701296

1271-
eq("File \"bs_array_test.ml\", line 288, characters 5-12", some2(/* :: */[
1297+
eq("File \"bs_array_test.ml\", line 291, characters 5-12", some2(/* :: */[
12721298
0,
12731299
/* :: */[
12741300
3,
@@ -1284,7 +1310,7 @@ eq("File \"bs_array_test.ml\", line 288, characters 5-12", some2(/* :: */[
12841310
return x > y;
12851311
})), true);
12861312

1287-
eq("File \"bs_array_test.ml\", line 293, characters 5-12", Belt_Array.concat(/* array */[], /* array */[
1313+
eq("File \"bs_array_test.ml\", line 296, characters 5-12", Belt_Array.concat(/* array */[], /* array */[
12881314
1,
12891315
2,
12901316
3
@@ -1294,9 +1320,9 @@ eq("File \"bs_array_test.ml\", line 293, characters 5-12", Belt_Array.concat(/*
12941320
3
12951321
]);
12961322

1297-
eq("File \"bs_array_test.ml\", line 294, characters 5-12", Belt_Array.concat(/* array */[], /* array */[]), /* array */[]);
1323+
eq("File \"bs_array_test.ml\", line 297, characters 5-12", Belt_Array.concat(/* array */[], /* array */[]), /* array */[]);
12981324

1299-
eq("File \"bs_array_test.ml\", line 295, characters 5-12", Belt_Array.concat(/* array */[
1325+
eq("File \"bs_array_test.ml\", line 298, characters 5-12", Belt_Array.concat(/* array */[
13001326
3,
13011327
2
13021328
], /* array */[
@@ -1311,7 +1337,7 @@ eq("File \"bs_array_test.ml\", line 295, characters 5-12", Belt_Array.concat(/*
13111337
3
13121338
]);
13131339

1314-
eq("File \"bs_array_test.ml\", line 296, characters 5-12", Belt_Array.concatMany(/* array */[
1340+
eq("File \"bs_array_test.ml\", line 299, characters 5-12", Belt_Array.concatMany(/* array */[
13151341
/* array */[
13161342
3,
13171343
2
@@ -1329,7 +1355,7 @@ eq("File \"bs_array_test.ml\", line 296, characters 5-12", Belt_Array.concatMany
13291355
3
13301356
]);
13311357

1332-
eq("File \"bs_array_test.ml\", line 297, characters 5-12", Belt_Array.concatMany(/* array */[
1358+
eq("File \"bs_array_test.ml\", line 300, characters 5-12", Belt_Array.concatMany(/* array */[
13331359
/* array */[
13341360
3,
13351361
2
@@ -1350,7 +1376,7 @@ eq("File \"bs_array_test.ml\", line 297, characters 5-12", Belt_Array.concatMany
13501376
0
13511377
]);
13521378

1353-
eq("File \"bs_array_test.ml\", line 298, characters 5-12", Belt_Array.concatMany(/* array */[
1379+
eq("File \"bs_array_test.ml\", line 301, characters 5-12", Belt_Array.concatMany(/* array */[
13541380
/* array */[],
13551381
/* array */[
13561382
3,
@@ -1372,12 +1398,12 @@ eq("File \"bs_array_test.ml\", line 298, characters 5-12", Belt_Array.concatMany
13721398
0
13731399
]);
13741400

1375-
eq("File \"bs_array_test.ml\", line 299, characters 5-12", Belt_Array.concatMany(/* array */[
1401+
eq("File \"bs_array_test.ml\", line 302, characters 5-12", Belt_Array.concatMany(/* array */[
13761402
/* array */[],
13771403
/* array */[]
13781404
]), /* array */[]);
13791405

1380-
b("File \"bs_array_test.ml\", line 302, characters 4-11", Belt_Array.cmp(/* array */[
1406+
b("File \"bs_array_test.ml\", line 305, characters 4-11", Belt_Array.cmp(/* array */[
13811407
1,
13821408
2,
13831409
3
@@ -1388,7 +1414,7 @@ b("File \"bs_array_test.ml\", line 302, characters 4-11", Belt_Array.cmp(/* arra
13881414
3
13891415
], Caml_obj.caml_compare) < 0);
13901416

1391-
b("File \"bs_array_test.ml\", line 303, characters 4-11", Belt_Array.cmp(/* array */[
1417+
b("File \"bs_array_test.ml\", line 306, characters 4-11", Belt_Array.cmp(/* array */[
13921418
0,
13931419
1,
13941420
2,
@@ -1399,7 +1425,7 @@ b("File \"bs_array_test.ml\", line 303, characters 4-11", Belt_Array.cmp(/* arra
13991425
3
14001426
], Caml_obj.caml_compare) > 0);
14011427

1402-
b("File \"bs_array_test.ml\", line 304, characters 4-11", Belt_Array.cmp(/* array */[
1428+
b("File \"bs_array_test.ml\", line 307, characters 4-11", Belt_Array.cmp(/* array */[
14031429
1,
14041430
2,
14051431
3
@@ -1409,7 +1435,7 @@ b("File \"bs_array_test.ml\", line 304, characters 4-11", Belt_Array.cmp(/* arra
14091435
2
14101436
], Caml_primitive.caml_int_compare) > 0);
14111437

1412-
b("File \"bs_array_test.ml\", line 305, characters 4-11", Belt_Array.cmp(/* array */[
1438+
b("File \"bs_array_test.ml\", line 308, characters 4-11", Belt_Array.cmp(/* array */[
14131439
1,
14141440
2,
14151441
3
@@ -1419,7 +1445,7 @@ b("File \"bs_array_test.ml\", line 305, characters 4-11", Belt_Array.cmp(/* arra
14191445
3
14201446
], Caml_primitive.caml_int_compare) === 0);
14211447

1422-
b("File \"bs_array_test.ml\", line 306, characters 4-11", Belt_Array.cmp(/* array */[
1448+
b("File \"bs_array_test.ml\", line 309, characters 4-11", Belt_Array.cmp(/* array */[
14231449
1,
14241450
2,
14251451
4
@@ -1429,7 +1455,7 @@ b("File \"bs_array_test.ml\", line 306, characters 4-11", Belt_Array.cmp(/* arra
14291455
3
14301456
], Caml_primitive.caml_int_compare) > 0);
14311457

1432-
Mt.from_pair_suites("File \"bs_array_test.ml\", line 309, characters 23-30", suites[0]);
1458+
Mt.from_pair_suites("File \"bs_array_test.ml\", line 312, characters 23-30", suites[0]);
14331459

14341460
var A = 0;
14351461

jscomp/test/bs_array_test.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,10 @@ let () =
234234
eq __LOC__ (A.zip [|1;2;3|] [|2;3;4;1|]) [|1,2;2,3;3,4|];
235235
eq __LOC__ (A.zip [|2;3;4;1|] [|1;2;3|] ) [|2,1;3,2;4,3|];
236236
eq __LOC__ (A.zipBy [|2;3;4;1|] [|1;2;3|] (-)) [|1;1;1|];
237-
eq __LOC__ (A.zipBy [|1;2;3|] [|2;3;4;1|] (-)) (A.map [|1;1;1|] (fun x -> -x))
237+
eq __LOC__ (A.zipBy [|1;2;3|] [|2;3;4;1|] (-)) (A.map [|1;1;1|] (fun x -> -x));
238+
eq __LOC__ (A.unzip [|1,2;2,3;3,4|]) ([|1;2;3|], [|2;3;4|])
239+
240+
(* Here *)
238241

239242
let sumUsingForEach xs =
240243
let v = ref 0 in

lib/js/belt_Array.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,21 @@ function cmp(a, b, p) {
541541
return cmpU(a, b, Curry.__2(p));
542542
}
543543

544+
function unzip(a) {
545+
var l = a.length;
546+
var a1 = new Array(l);
547+
var a2 = new Array(l);
548+
for(var i = 0 ,i_finish = l - 1 | 0; i <= i_finish; ++i){
549+
var match = a[i];
550+
a1[i] = match[0];
551+
a2[i] = match[1];
552+
}
553+
return /* tuple */[
554+
a1,
555+
a2
556+
];
557+
}
558+
544559
exports.get = get;
545560
exports.getExn = getExn;
546561
exports.set = set;
@@ -559,6 +574,7 @@ exports.makeByAndShuffle = makeByAndShuffle;
559574
exports.zip = zip;
560575
exports.zipByU = zipByU;
561576
exports.zipBy = zipBy;
577+
exports.unzip = unzip;
562578
exports.concat = concat;
563579
exports.concatMany = concatMany;
564580
exports.slice = slice;

0 commit comments

Comments
 (0)