8000 added shuffle to list · immutable-js/immutable-js@98c5923 · GitHub
[go: up one dir, main page]

Skip to content

Commit 98c5923

Browse files
committed
added shuffle to list
1 parent 6ee34e1 commit 98c5923

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed

__tests__/List.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { fromJS, List, Map, Range, Seq, Set } from 'immutable';
22
import * as jasmineCheck from 'jasmine-check';
3+
import { create as createSeed } from 'random-seed';
34

45
jasmineCheck.install();
56

@@ -1012,6 +1013,31 @@ describe('List', () => {
10121013
});
10131014
});
10141015

1016+
describe('when shuffling', () => {
1017+
const list = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
1018+
1019+
it('should work when empty', () => {
1020+
expect(List().shuffle()).toStrictEqual(List());
1021+
});
1022+
it('should work with Math.random', () => {
1023+
expect(list.shuffle().sort()).toStrictEqual(list);
1024+
});
1025+
it('should work with a pseudo random number generator', () => {
1026+
const seed = createSeed('lorem ipsum');
1027+
const random = () => seed.random();
1028+
1029+
expect(list.shuffle(random)).toStrictEqual(
1030+
List.of(5, 2, 4, 7, 6, 3, 10, 1, 9, 8)
1031+
);
1032+
expect(list.shuffle(random)).toStrictEqual(
1033+
List.of(1, 6, 2, 3, 9, 7, 4, 10, 5, 8)
1034+
);
1035+
expect(list.shuffle(random)).toStrictEqual(
1036+
List.of(6, 1, 8, 10, 9, 5, 4, 7, 3, 2)
1037+
);
1038+
});
1039+
});
1040+
10151041
describe('Iterator', () => {
10161042
const pInt = gen.posInt;
10171043

package-lock.json

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
"@size-limit/preset-small-lib": "^8.2.6",
9393
"@types/jest": "^29.0.0",
9494
"@types/prismjs": "^1.26.3",
95+
"@types/random-seed": "0.3.5",
9596
"@types/react": "17.0.11",
9697
"benchmark": "2.1.4",
9798
"colors": "1.4.0",
@@ -116,6 +117,7 @@
116117
"npm-run-all": "4.1.5",
117118
"prettier": "^3.5.0",
118119
"prismjs": "^1.29.0",
120+
"random-seed": "0.3.0",
119121
"react": "^18.2.0",
120122
"react-dom": "^18.2.0",
121123
"rimraf": "2.7.1",

src/List.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,22 @@ export class List extends IndexedCollection {
140140
return setListBounds(this, 1);
141141
}
142142

143+
shuffle(random = Math.random) {
144+
return this.withMutations((mutable) => {
145+
let current = mutable.size,
146+
destination,
147+
tmp;
148+
149+
while (current) {
150+
destination = Math.floor(random() * current--);
151+
152+
tmp = mutable.get(destination);
153+
mutable.set(destination, mutable.get(current));
154+
mutable.set(current, tmp);
155+
}
156+
});
157+
}
158+
143159
// @pragma Composition
144160

145161
concat(/*...collections*/) {

type-definitions/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
/**

0 commit comments

Comments
 (0)
0