Skip to content

Commit 096d7ff

Browse files
author
josh
committed
Add flatMap to Belt.Array
1 parent d926d69 commit 096d7ff

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

jscomp/others/belt_Array.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,11 @@ let mapU a f =
267267

268268
let map a f = mapU a (fun[@bs] a -> f a)
269269

270+
let flatMapU a f =
271+
concatMany (mapU a f)
272+
273+
let flatMap a f = flatMapU a (fun[@bs] a -> f a)
274+
270275
let getByU a p =
271276
let l = length a in
272277
let i = ref 0 in

jscomp/others/belt_Array.mli

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,19 @@ val map: 'a array -> ('a -> 'b ) -> 'b array
395395
```
396396
*)
397397

398+
val flatMapU: 'a array -> ('a -> 'b array [@bs]) -> 'b array
399+
val flatMap: 'a array -> ('a -> 'b array) -> 'b array
400+
(**
401+
`flatMap xs f `
402+
403+
**return** a new array by calling `f` for each element of `xs` from
404+
the beginning to end, and then concatenating the results
405+
406+
```
407+
flatMap [|1;2|] (fun x-> [|x + 10;x + 20|]) = [|11;21;12;22|]
408+
```
409+
*)
410+
398411
val getByU: 'a array -> ('a -> bool [@bs]) -> 'a option
399412
val getBy: 'a array -> ('a -> bool) -> 'a option
400413
(**

lib/js/belt_Array.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,14 @@ function map(a, f) {
315315
return mapU(a, Curry.__1(f));
316316
}
317317

318+
function flatMapU(a, f) {
319+
return concatMany(map(a, f));
320+
}
321+
322+
function flatMap(a, f) {
323+
return flatMapU(a, Curry.__1(f))
324+
}
325+
318326
function getByU(a, p) {
319327
var l = a.length;
320328
var i = 0;
@@ -706,6 +714,8 @@ exports.forEachU = forEachU;
706714
exports.forEach = forEach;
707715
exports.mapU = mapU;
708716
exports.map = map;
717+
exports.flatMapU = flatMapU;
718+
exports.flatMap = flatMap;
709719
exports.getByU = getByU;
710720
exports.getBy = getBy;
711721
exports.getIndexByU = getIndexByU;

0 commit comments

Comments
 (0)