fast-shuffle
A fast implementation of a fisher-yates shuffle that does not mutate the source array.
Last updated 2 years ago by philihp .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install fast-shuffle 
SYNC missed versions from official npm registry.

Fast Shuffle

Version Tests Coverage Status Downloads License

A fast, side-effect-free, and O(n) array shuffle that's safe for functional programming and use within Redux reducers.

Usage

npm install --save fast-shuffle
import { shuffle } from 'fast-shuffle'

const suits = ['♣', '♦', '♥', '♠']
const faces = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A']
const sortedDeck = suits.map((suit) => faces.map((face) => face + suit)).flat()
// [ '2♣', '3♣', '4♣', '5♣', '6♣', '7♣', '8♣', ...

const shuffledDeck = shuffle(sortedDeck)
// [ '3♥', '3♦', 'K♥', '6♦', 'J♣', '5♠', 'A♠', ...

The parameters are also curried, so it can be used in pipelines.

import { shuffle } from 'fast-shuffle'

const randomCapitalLetter =
  ['a', 'b', 'c', 'd', 'e', 'f']   // :: () -> [a]
  |> shuffle,                      // :: [a] -> [a]
  |> _ => _[0]                     // :: [a] -> a
  |> _ => _.toUpperCase()          // :: a -> a

The named shuffle export seen above uses Math.random for entropy. If you import it without the brackets, you'll get a deterministic shuffler which takes an int for its random seed (e.g. Date.now()).

import shuffle from 'fast-shuffle' // note the change

const letters = ['a', 'b', 'c', 'd', 'e']
const shuffleRed = shuffle(12345)
shuffleRed(letters) // [ 'a', 'b', 'c', 'd', 'e' ]
shuffleRed(letters) // [ 'a', 'd', 'b', 'e', 'c' ]
shuffleRed(letters) // [ 'c', 'a', 'e', 'b', 'd' ]
shuffleRed(letters) // [ 'b', 'c', 'e', 'a', 'd' ]

const shuffleBlue = shuffle(12345)
shuffleBlue(letters) // [ 'a', 'b', 'c', 'd', 'e' ]
shuffleBlue(letters) // [ 'a', 'd', 'b', 'e', 'c' ]
shuffleBlue(letters) // [ 'c', 'a', 'e', 'b', 'd' ]
shuffleBlue(letters) // [ 'b', 'c', 'e', 'a', 'd' ]

If you give it an array of your array and a random seed, you'll get a shuffled array and a new random seed back. This is a pure function, so you can use it in your Redux reducers.

import { SHUFFLE_DECK } from './actions'
import shuffle from 'fast-shuffle'

const initialState = {
  ...
  deck: ['♣', '♦', '♥', '♠'],
  randomizer: Date.now()
}

const dealerApp = (state = initialState, action) => {
  switch (action.type) {
    ...
    case SHUFFLE_DECK:
      const [ deck, randomizer ] = shuffle([state.deck, state.randomizer])
      return {
        ...state,
        deck,
        randomizer,
      }
    ...
    default:
      return state
  }
}

Shuffle doesn't mutate the original array, instead it gives you back a shallow copy. This is important for React and performance reasons.

Why not use existing libraries?

  1. It doesn't mutate your source array, so it's safe for Redux reducers.

  2. The parameters are curried in the correct order, so you can use it within |> or Ramda pipes.

  3. It's stupid-fast and scales to large arrays without breaking a sweat.

  4. You can BYO-RNG.

Current Tags

  • 4.2.2                                ...           latest (3 months ago)

15 Versions

  • 4.2.2                                ...           3 months ago
  • 4.2.1                                ...           3 months ago
  • 4.2.0                                ...           3 months ago
  • 4.1.0                                ...           3 months ago
  • 4.0.0                                ...           3 months ago
  • 3.0.0                                ...           5 months ago
  • 2.0.2                                ...           5 months ago
  • 2.0.1                                ...           6 months ago
  • 2.0.0                                ...           6 months ago
  • 1.0.5                                ...           2 years ago
  • 1.0.4                                ...           3 years ago
  • 1.0.3                                ...           3 years ago
  • 1.0.2                                ...           3 years ago
  • 1.0.1                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (6)
Dependents (1)

Copyright 2014 - 2016 © taobao.org |