Skip to content

Commit 0d35f59

Browse files
authored
Merge pull request rescript-lang#3334 from bloodyowl/array-getby
Add Array.getBy
2 parents 14ebdb8 + 1e57631 commit 0d35f59

File tree

5 files changed

+67
-2
lines changed

5 files changed

+67
-2
lines changed

jscomp/others/belt_Array.ml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,22 @@ let mapU a f =
265265

266266
let map a f = mapU a (fun[@bs] a -> f a)
267267

268+
let getByU a p =
269+
let l = length a in
270+
let i = ref 0 in
271+
let r = ref None in
272+
while !r = None && !i < l do
273+
let v = (getUnsafe a !i) in
274+
if p v [@bs] then
275+
begin
276+
r := Some v;
277+
end;
278+
incr i
279+
done;
280+
!r
281+
282+
let getBy a p = getByU a (fun[@bs] a -> p a)
283+
268284
let keepU a f =
269285
let l = length a in
270286
let r = makeUninitializedUnsafe l in

jscomp/others/belt_Array.mli

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,16 @@ val map: 'a array -> ('a -> 'b ) -> 'b array
369369
370370
*)
371371

372+
val getByU: 'a array -> ('a -> bool [@bs]) -> 'a option
373+
val getBy: 'a array -> ('a -> bool) -> 'a option
374+
(** [getBy xs p] returns [Some value] for the first value in [xs] that satisifies the predicate function [p]; returns [None] if no element satisifies the function.
375+
376+
@example {[
377+
getBy [|1;4;3;2|] (fun x -> x mod 2 = 0) = Some 4
378+
getBy [|15;13;11|] (fun x -> x mod 2 = 0) = None
379+
]}
380+
*)
381+
372382
val keepU: 'a array -> ('a -> bool [@bs]) -> 'a array
373383
val keep: 'a array -> ('a -> bool ) -> 'a array
374384
(** [keep xs p ]

jscomp/test/bs_array_test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1542,7 +1542,23 @@ b("File \"bs_array_test.ml\", line 331, characters 4-11", Belt_Array.cmp(/* arra
15421542
3
15431543
], Caml_primitive.caml_int_compare) > 0);
15441544

1545-
Mt.from_pair_suites("File \"bs_array_test.ml\", line 334, characters 23-30", suites[0]);
1545+
eq("File \"bs_array_test.ml\", line 334, characters 5-12", Belt_Array.getBy(/* array */[
1546+
1,
1547+
2,
1548+
3
1549+
], (function (x) {
1550+
return x > 1;
1551+
})), 2);
1552+
1553+
eq("File \"bs_array_test.ml\", line 335, characters 5-12", Belt_Array.getBy(/* array */[
1554+
1,
1555+
2,
1556+
3
1557+
], (function (x) {
1558+
return x > 3;
1559+
})), undefined);
1560+
1561+
Mt.from_pair_suites("File \"bs_array_test.ml\", line 337, characters 23-30", suites[0]);
15461562

15471563
var A = 0;
15481564

jscomp/test/bs_array_test.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,10 @@ let () =
328328
b __LOC__ (A.cmp [|0;1;2;3|] [|1;2;3|] compare > 0) ;
329329
b __LOC__ (A.cmp [|1;2;3|] [|0;1;2|] (fun x y -> compare x y) > 0);
330330
b __LOC__ (A.cmp [|1;2;3|] [|1;2;3|] (fun x y -> compare x y) = 0);
331-
b __LOC__ (A.cmp [|1;2;4|] [|1;2;3|] (fun x y -> compare x y) > 0);
331+
b __LOC__ (A.cmp [|1;2;4|] [|1;2;3|] (fun x y -> compare x y) > 0)
332332

333+
let () =
334+
eq __LOC__ (A.getBy [|1;2;3|] (fun x -> x > 1)) (Some 2);
335+
eq __LOC__ (A.getBy [|1;2;3|] (fun x -> x > 3)) None;
333336

334337
;; Mt.from_pair_suites __LOC__ !suites

lib/js/belt_Array.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,24 @@ function map(a, f) {
311311
return mapU(a, Curry.__1(f));
312312
}
313313

314+
function getByU(a, p) {
315+
var l = a.length;
316+
var i = 0;
317+
var r = undefined;
318+
while(r === undefined && i < l) {
319+
var v = a[i];
320+
if (p(v)) {
321+
r = Caml_option.some(v);
322+
}
323+
i = i + 1 | 0;
324+
};
325+
return r;
326+
}
327+
328+
function getBy(a, p) {
329+
return getByU(a, Curry.__1(p));
330+
}
331+
314332
function keepU(a, f) {
315333
var l = a.length;
316334
var r = new Array(l);
@@ -657,6 +675,8 @@ exports.forEachU = forEachU;
657675
exports.forEach = forEach;
658676
exports.mapU = mapU;
659677
exports.map = map;
678+
exports.getByU = getByU;
679+
exports.getBy = getBy;
660680
exports.keepU = keepU;
661681
exports.keep = keep;
662682
exports.keepWithIndexU = keepWithIndexU;

0 commit comments

Comments
 (0)