Simplify shuffle and sample.

This commit is contained in:
John-David Dalton
2017-02-06 23:53:17 -08:00
parent b0980a90fc
commit cb7612aef6
11 changed files with 44 additions and 162 deletions

View File

@@ -1,15 +0,0 @@
import baseRandom from './baseRandom.js'
/**
* A specialized version of `sample` for arrays.
*
* @private
* @param {Array} array The array to sample.
* @returns {*} Returns the random element.
*/
function arraySample(array) {
const length = array.length
return length ? array[baseRandom(0, length - 1)] : undefined
}
export default arraySample

View File

@@ -1,17 +0,0 @@
import baseClamp from './baseClamp.js'
import copyArray from './copyArray.js'
import shuffleSelf from './shuffleSelf.js'
/**
* A specialized version of `sampleSize` for arrays.
*
* @private
* @param {Array} array The array to sample.
* @param {number} n The number of elements to sample.
* @returns {Array} Returns the random elements.
*/
function arraySampleSize(array, n) {
return shuffleSelf(copyArray(array), baseClamp(n, 0, array.length))
}
export default arraySampleSize

View File

@@ -1,15 +0,0 @@
import copyArray from './copyArray.js'
import shuffleSelf from './shuffleSelf.js'
/**
* A specialized version of `shuffle` for arrays.
*
* @private
* @param {Array} array The array to shuffle.
* @returns {Array} Returns the new shuffled array.
*/
function arrayShuffle(array) {
return shuffleSelf(copyArray(array))
}
export default arrayShuffle

View File

@@ -1,14 +0,0 @@
/**
* The base implementation of `random` without support for returning
* floating-point numbers.
*
* @private
* @param {number} lower The lower bound.
* @param {number} upper The upper bound.
* @returns {number} Returns the random number.
*/
function baseRandom(lower, upper) {
return lower + Math.floor(Math.random() * (upper - lower + 1))
}
export default baseRandom

View File

@@ -1,15 +0,0 @@
import arraySample from './arraySample.js'
import values from '../values.js'
/**
* The base implementation of `sample`.
*
* @private
* @param {Array|Object} collection The collection to sample.
* @returns {*} Returns the random element.
*/
function baseSample(collection) {
return arraySample(values(collection))
}
export default baseSample

View File

@@ -1,18 +0,0 @@
import baseClamp from './baseClamp.js'
import shuffleSelf from './shuffleSelf.js'
import values from '../values.js'
/**
* The base implementation of `sampleSize` without param guards.
*
* @private
* @param {Array|Object} collection The collection to sample.
* @param {number} n The number of elements to sample.
* @returns {Array} Returns the random elements.
*/
function baseSampleSize(collection, n) {
const array = values(collection)
return shuffleSelf(array, baseClamp(n, 0, array.length))
}
export default baseSampleSize

View File

@@ -1,15 +0,0 @@
import shuffleSelf from './shuffleSelf.js'
import values from '../values.js'
/**
* The base implementation of `shuffle`.
*
* @private
* @param {Array|Object} collection The collection to shuffle.
* @returns {Array} Returns the new shuffled array.
*/
function baseShuffle(collection) {
return shuffleSelf(values(collection))
}
export default baseShuffle

View File

@@ -1,28 +0,0 @@
import baseRandom from './baseRandom.js'
/**
* A specialized version of `shuffle` which mutates and sets the size of `array`.
*
* @private
* @param {Array} array The array to shuffle.
* @param {number} [size=array.length] The size of `array`.
* @returns {Array} Returns `array`.
*/
function shuffleSelf(array, size) {
let index = -1
const length = array.length
const lastIndex = length - 1
size = size === undefined ? length : size
while (++index < size) {
const rand = baseRandom(index, lastIndex)
const value = array[rand]
array[rand] = array[index]
array[index] = value
}
array.length = size
return array
}
export default shuffleSelf

View File

@@ -1,21 +1,18 @@
import arraySample from './.internal/arraySample.js'
import baseSample from './.internal/baseSample.js'
/**
* Gets a random element from `collection`.
* Gets a random element from `array`.
*
* @since 2.0.0
* @category Collection
* @param {Array|Object} collection The collection to sample.
* @category Array
* @param {Array} array The array to sample.
* @returns {*} Returns the random element.
* @example
*
* sample([1, 2, 3, 4])
* // => 2
*/
function sample(collection) {
const func = Array.isArray(collection) ? arraySample : baseSample
return func(collection)
function sample(array) {
const length = array == null ? 0 : array.length
return length ? array[Math.floor(Math.random() * length)] : undefined
}
export default sample

View File

@@ -1,15 +1,13 @@
import arraySampleSize from './.internal/arraySampleSize.js'
import baseSampleSize from './.internal/baseSampleSize.js'
import isIterateeCall from './.internal/isIterateeCall.js'
import toInteger from './toInteger.js'
/**
* Gets `n` random elements at unique keys from `collection` up to the
* size of `collection`.
* Gets `n` random elements at unique keys from `array` up to the
* size of `array`.
*
* @since 4.0.0
* @category Collection
* @param {Array|Object} collection The collection to sample.
* @category Array
* @param {Array} array The array to sample.
* @param {number} [n=1] The number of elements to sample.
* @param- {Object} [guard] Enables use as an iteratee for methods like `map`.
* @returns {Array} Returns the random elements.
@@ -21,14 +19,27 @@ import toInteger from './toInteger.js'
* sampleSize([1, 2, 3], 4)
* // => [2, 3, 1]
*/
function sampleSize(collection, n, guard) {
function sampleSize(array, n, guard) {
if ((guard ? isIterateeCall(collection, n, guard) : n === undefined)) {
n = 1
} else {
n = toInteger(n)
}
const func = Array.isArray(collection) ? arraySampleSize : baseSampleSize
return func(collection, n)
const length = array == null ? 0 : array.length
if (!length || n < 1) {
return []
}
n = n > length ? length : n
let index = -1
const lastIndex = n - 1
const result = copyArray(array)
while (++index < n) {
const rand = index + Math.floor(Math.random() * (lastIndex - index + 1))
const value = result[rand]
result[rand] = result[index]
result[index] = value
}
return result
}
export default sampleSize

View File

@@ -1,22 +1,33 @@
import arrayShuffle from './.internal/arrayShuffle.js'
import baseShuffle from './.internal/baseShuffle.js'
import copyArray from './copyArray.js'
/**
* Creates an array of shuffled values, using a version of the
* [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher-Yates_shuffle).
*
* @since 0.1.0
* @category Collection
* @param {Array|Object} collection The collection to shuffle.
* @category Array
* @param {Array} array The array to shuffle.
* @returns {Array} Returns the new shuffled array.
* @example
*
* shuffle([1, 2, 3, 4])
* // => [4, 1, 3, 2]
*/
function shuffle(collection) {
const func = Array.isArray(collection) ? arrayShuffle : baseShuffle
return func(collection)
function shuffle(array) {
const length = array == null ? 0 : array.length
if (!length) {
return []
}
let index = -1
const lastIndex = length - 1
const result = copyArray(array)
while (++index < length) {
const rand = index + Math.floor(Math.random() * (lastIndex - index + 1))
const value = result[rand]
result[rand] = result[index]
result[index] = value
}
return result
}
export default shuffle