Skip to content

Update ST FFI to use StFns #234

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 4 commits into from
Jun 13, 2023
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 @@ -20,6 +20,7 @@ New features:

Other improvements:
- Use more efficient implementation for `mapWithIndex` (#233 by @JordanMartinez)
- Updates `ST` FFI to use uncurried functions via `STFnX` types (#234 by @JordanMartinez)

## [v7.1.0](https://github.com/purescript/purescript-arrays/releases/tag/v7.1.0) - 2022-08-06

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"purescript-partial": "^4.0.0",
"purescript-prelude": "^6.0.0",
"purescript-safe-coerce": "^2.0.0",
"purescript-st": "^6.0.0",
"purescript-st": "^6.2.0",
"purescript-tailrec": "^6.0.0",
"purescript-tuples": "^7.0.0",
"purescript-unfoldable": "^6.0.0",
Expand Down
135 changes: 38 additions & 97 deletions src/Data/Array/ST.js
Original file line number Diff line number Diff line change
@@ -1,107 +1,57 @@
function newSTArray () {
function newSTArray() {
return [];
}
export { newSTArray as new };

export const peekImpl = function (just) {
return function (nothing) {
return function (i) {
return function (xs) {
return function () {
return i >= 0 && i < xs.length ? just(xs[i]) : nothing;
};
};
};
};
export const peekImpl = function (just, nothing, i, xs) {
return i >= 0 && i < xs.length ? just(xs[i]) : nothing;
};

export const poke = function (i) {
return function (a) {
return function (xs) {
return function () {
var ret = i >= 0 && i < xs.length;
if (ret) xs[i] = a;
return ret;
};
};
};
export const pokeImpl = function (i, a, xs) {
var ret = i >= 0 && i < xs.length;
if (ret) xs[i] = a;
return ret;
};

export const length = function (xs) {
return function () {
return xs.length;
};
export const lengthImpl = function (xs) {
return xs.length;
};

export const popImpl = function (just) {
return function (nothing) {
return function (xs) {
return function () {
return xs.length > 0 ? just(xs.pop()) : nothing;
};
};
};
export const popImpl = function (just, nothing, xs) {
return xs.length > 0 ? just(xs.pop()) : nothing;
};

export const pushAll = function (as) {
return function (xs) {
return function () {
return xs.push.apply(xs, as);
};
};
export const pushAllImpl = function (as, xs) {
return xs.push.apply(xs, as);
};

export const shiftImpl = function (just) {
return function (nothing) {
return function (xs) {
return function () {
return xs.length > 0 ? just(xs.shift()) : nothing;
};
};
};
export const shiftImpl = function (just, nothing, xs) {
return xs.length > 0 ? just(xs.shift()) : nothing;
};

export const unshiftAll = function (as) {
return function (xs) {
return function () {
return xs.unshift.apply(xs, as);
};
};
export const unshiftAllImpl = function (as, xs) {
return xs.unshift.apply(xs, as);
};

export const splice = function (i) {
return function (howMany) {
return function (bs) {
return function (xs) {
return function () {
return xs.splice.apply(xs, [i, howMany].concat(bs));
};
};
};
};
export const spliceImpl = function (i, howMany, bs, xs) {
return xs.splice.apply(xs, [i, howMany].concat(bs));
};

export const unsafeFreeze = function (xs) {
return function () {
return xs;
};
};
function unsafeFreezeThawImpl(xs) {
return xs;
}

export const unsafeThaw = function (xs) {
return function () {
return xs;
};
};
export const unsafeFreezeImpl = unsafeFreezeThawImpl;

export const unsafeThawImpl = unsafeFreezeThawImpl;

function copyImpl(xs) {
return function () {
return xs.slice();
};
return xs.slice();
}

export const freeze = copyImpl;
export const freezeImpl = copyImpl;

export const thaw = copyImpl;
export const thawImpl = copyImpl;

export const sortByImpl = (function () {
function mergeFromTo(compare, fromOrdering, xs1, xs2, from, to) {
Expand All @@ -127,8 +77,7 @@ export const sortByImpl = (function () {
if (c > 0) {
xs1[k++] = y;
++j;
}
else {
} else {
xs1[k++] = x;
++i;
}
Expand All @@ -141,26 +90,18 @@ export const sortByImpl = (function () {
}
}

return function (compare) {
return function (fromOrdering) {
return function (xs) {
return function () {
if (xs.length < 2) return xs;
return function (compare, fromOrdering, xs) {
if (xs.length < 2) return xs;

mergeFromTo(compare, fromOrdering, xs, xs.slice(0), 0, xs.length);
mergeFromTo(compare, fromOrdering, xs, xs.slice(0), 0, xs.length);

return xs;
};
};
};
return xs;
};
})();

export const toAssocArray = function (xs) {
return function () {
var n = xs.length;
var as = new Array(n);
for (var i = 0; i < n; i++) as[i] = { value: xs[i], index: i };
return as;
};
export const toAssocArrayImpl = function (xs) {
var n = xs.length;
var as = new Array(n);
for (var i = 0; i < n; i++) as[i] = { value: xs[i], index: i };
return as;
};
Loading