Skip to content

Avoid RangeError in arrayBind foreign implementation #314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Breaking changes:
New features:

Bugfixes:
- Avoid `RangeError` in `arrayBind` foreign implementation (#314 by @pete-murphy)

Other improvements:

Expand Down
28 changes: 20 additions & 8 deletions src/Control/Bind.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
export const arrayBind = function (arr) {
return function (f) {
var result = [];
for (var i = 0, l = arr.length; i < l; i++) {
Array.prototype.push.apply(result, f(arr[i]));
export const arrayBind =
typeof Array.prototype.flatMap === "function"
? function (arr) {
return function (f) {
return arr.flatMap(f);
};
}
return result;
};
};
: function (arr) {
return function (f) {
var result = [];
var l = arr.length;
for (var i = 0; i < l; i++) {
var xs = f(arr[i]);
var k = xs.length;
for (var j = 0; j < k; j++) {
result.push(xs[j]);
}
}
return result;
};
};
8 changes: 8 additions & 0 deletions test/Test/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ export function testNumberShow(showNumber) {
]);
};
}

export function makeArray(length) {
var arr = [];
for (var i = 0; i < length; i++) {
arr.push(i);
}
return arr;
}
12 changes: 12 additions & 0 deletions test/Test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ main = do
testReflectType
testReifyType
testSignum
testArrayBind

foreign import testNumberShow :: (Number -> String) -> AlmostEff

Expand Down Expand Up @@ -189,3 +190,14 @@ testSignum = do
assert "signum positive zero" $ show (1.0/(signum 0.0)) == "Infinity"
assert "Clarifies what 'signum negative zero' test is doing" $ show (1.0/(-0.0)) == "-Infinity"
assert "signum negative zero" $ show (1.0/(signum (-0.0))) == "-Infinity"

foreign import makeArray :: Int -> Array Int

testArrayBind :: AlmostEff
testArrayBind = do
assert "Array bind does not cause RangeError" do
let
_ = do
_ <- [unit]
makeArray 106_000
true