File tree Expand file tree Collapse file tree 4 files changed +47
-1
lines changed Expand file tree Collapse file tree 4 files changed +47
-1
lines changed Original file line number Diff line number Diff line change @@ -758,6 +758,14 @@ declare namespace Immutable {
758
758
zipper : ( ...values : Array < unknown > ) => Z ,
759
759
...collections : Array < Collection < unknown , unknown > >
760
760
) : 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;
761
769
}
762
770
763
771
/**
Original file line number Diff line number Diff line change @@ -3469,6 +3469,25 @@ var List = /*@__PURE__*/(function (IndexedCollection) {
3469
3469
return setListBounds ( this , 1 ) ;
3470
3470
} ;
3471
3471
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
+
3472
3491
// @pragma Composition
3473
3492
3474
3493
List . prototype . concat = function concat ( /*...collections*/ ) {
Original file line number Diff line number Diff line change 3475
3475
return setListBounds ( this , 1 ) ;
3476
3476
} ;
3477
3477
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
+
3478
3497
// @pragma Composition
3479
3498
3480
3499
List . prototype . concat = function concat ( /*...collections*/ ) {
You can’t perform that action at this time.
0 commit comments