Skip to content

Commit 77b1b54

Browse files
deploy: c589858
1 parent 64d1f06 commit 77b1b54

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

dist/immutable.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,14 @@ declare namespace Immutable {
758758
zipper: (...values: Array<unknown>) => Z,
759759
...collections: Array<Collection<unknown, unknown>>
760760
): List<Z>;
761+
762+
/**
763+
* Returns a new List with its values shuffled thanks to the
764+
* [Fisher–Yates](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle)
765+
* algorithm.
766+
* It uses Math.random, but you can provide your own random number generator.
767+
*/
768+
shuffle(random?: () => number): this;
761769
}
762770

763771
/**

dist/immutable.es.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3469,6 +3469,25 @@ var List = /*@__PURE__*/(function (IndexedCollection) {
34693469
return setListBounds(this, 1);
34703470
};
34713471

3472+
List.prototype.shuffle = function shuffle (random) {
3473+
if ( random === void 0 ) random = Math.random;
3474+
3475+
return this.withMutations(function (mutable) {
3476+
// implementation of the Fisher-Yates shuffle: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
3477+
var current = mutable.size;
3478+
var destination;
3479+
var tmp;
3480+
3481+
while (current) {
3482+
destination = Math.floor(random() * current--);
3483+
3484+
tmp = mutable.get(destination);
3485+
mutable.set(destination, mutable.get(current));
3486+
mutable.set(current, tmp);
3487+
}
3488+
});
3489+
};
3490+
34723491
// @pragma Composition
34733492

34743493
List.prototype.concat = function concat (/*...collections*/) {

dist/immutable.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3475,6 +3475,25 @@
34753475
return setListBounds(this, 1);
34763476
};
34773477

3478+
List.prototype.shuffle = function shuffle (random) {
3479+
if ( random === void 0 ) random = Math.random;
3480+
3481+
return this.withMutations(function (mutable) {
3482+
// implementation of the Fisher-Yates shuffle: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
3483+
var current = mutable.size;
3484+
var destination;
3485+
var tmp;
3486+
3487+
while (current) {
3488+
destination = Math.floor(random() * current--);
3489+
3490+
tmp = mutable.get(destination);
3491+
mutable.set(destination, mutable.get(current));
3492+
mutable.set(current, tmp);
3493+
}
3494+
});
3495+
};
3496+
34783497
// @pragma Composition
34793498

34803499
List.prototype.concat = function concat (/*...collections*/) {

dist/immutable.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)