Skip to content

Commit

Permalink
Fix #513: Improve core/js Shuffle algorithm (#519)
Browse files Browse the repository at this point in the history
  • Loading branch information
kigiri authored Apr 30, 2024
1 parent 6b2daf7 commit 1c4b82f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

[Squint](https://github.com/squint-cljs/squint): Light-weight ClojureScript dialect

## Unreleased

- [#513](https://github.com/squint-cljs/squint/issues/513): Fix `shuffle` core function random distribution and performances

## v0.7.105 (2024-04-15)

- [#509](https://github.com/squint-cljs/squint/issues/509): Optimization: use arrow fn for implicit IIFE when possible
Expand Down
13 changes: 10 additions & 3 deletions src/squint/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1849,9 +1849,16 @@ export function sort_by(keyfn, comp, coll) {
}

export function shuffle(coll) {
return [...coll].sort(function (_a, _b) {
return Math.random() - 0.5;
});
const result = [...coll];
let remaining = coll.length;
while (remaining) {
const i = Math.floor(Math.random() * remaining--);
const tmp = result[remaining];
result[remaining] = result[i];
result[i] = tmp;
}

return result;
}

export function some(pred, coll) {
Expand Down

0 comments on commit 1c4b82f

Please sign in to comment.