Necrotizing Pineapple Music

# npm

## generatorics

1.1.0 • Public • Published

# Generatorics

### An efficient combinatorics library for JavaScript utilizing ES2015 generators. Generate combinations, permutations, and power sets of arrays or strings.

• Node
``````npm install generatorics
``````
• Browser
``````bower install generatorics
``````

Note: This module is not transpiled for compatibility, as it degrades the performance. Check your browser/node version.

## Usage

### combination

For efficiency, each array being yielded is the same one being mutated on each iteration. DO NOT mutate the array.

You can clone if necessary, or use the clone submodule

## Clone Submodule

Each array yielded from the generator is actually the same array in memory, just mutated to have different elements. This is to avoid the unnecessary creation of a bunch of arrays, which consume memory. As a result, you get a strange result when trying to generate an array.

Instead, you can use the clone submodule.

### G.clone

This submodule produces generators that yield a different array on each iteration in case you need to mutate it. The combination, permutation, powerSet, permutationCombination, baseN, baseNAll, and cartesian methods are provided on this submodule.

## Cool things to do with ES2015 generators

#### Writing a code generator? Need to produce an infinite stream of minified variable names?

No problem! Just pass in a collection of all your valid characters and start generating.

## G

### G.factorial(n) ⇒ `Number`

Calculates a factorial

Kind: static method of `G`
Returns: `Number` - n!

Param Type Description
n `Number` The number to operate the factorial on.

### G.factoradic(n) ⇒ `Array`

Converts a number to the factorial number system. Digits are in least significant order.

Kind: static method of `G`
Returns: `Array` - digits of n in factoradic in least significant order

Param Type Description
n `Number` Integer in base 10

### G.P(n, k) ⇒ `Number`

Calculates the number of possible permutations of "k" elements in a set of size "n".

Kind: static method of `G`
Returns: `Number` - n P k

Param Type Description
n `Number` Number of elements in the set.
k `Number` Number of elements to choose from the set.

### G.C(n, k) ⇒ `Number`

Calculates the number of possible combinations of "k" elements in a set of size "n".

Kind: static method of `G`
Returns: `Number` - n C k

Param Type Description
n `Number` Number of elements in the set.
k `Number` Number of elements to choose from the set.

### G.choices(n, k, [options]) ⇒ `Number`

Higher level method for counting number of possible combinations of "k" elements from a set of size "n".

Kind: static method of `G`
Returns: `Number` - Number of possible combinations.

Param Type Description
n `Number` Number of elements in the set.
k `Number` Number of elements to choose from the set.
[options] `Object`
options.replace `Boolean` Is replacement allowed after each choice?
options.ordered `Boolean` Does the order of the choices matter?

### G.combination(arr, [size]) ⇒ `Generator`

Generates all combinations of a set.

Kind: static method of `G`
Returns: `Generator` - yields each combination as an array

Param Type Default Description
arr `Array` | `String` The set of elements.
[size] `Number` `arr.length` Number of elements to choose from the set.

### G.permutation(arr, [size]) ⇒ `Generator`

Generates all permutations of a set.

Kind: static method of `G`
Returns: `Generator` - yields each permutation as an array

Param Type Default Description
arr `Array` | `String` The set of elements.
[size] `Number` `arr.length` Number of elements to choose from the set.

### G.powerSet(arr) ⇒ `Generator`

Generates all possible subsets of a set (a.k.a. power set).

Kind: static method of `G`
Returns: `Generator` - yields each subset as an array

Param Type Description
arr `Array` | `String` The set of elements.

### G.permutationCombination(arr) ⇒ `Generator`

Generates the permutation of the combinations of a set.

Kind: static method of `G`
Returns: `Generator` - yields each permutation as an array

Param Type Description
arr `Array` | `String` The set of elements.

### G.baseN(arr, [size]) ⇒ `Generator`

Generates all possible "numbers" from the digits of a set.

Kind: static method of `G`
Returns: `Generator` - yields all digits as an array

Param Type Default Description
arr `Array` | `String` The set of digits.
[size] `Number` `arr.length` How many digits will be in the numbers.

### G.baseNAll(arr) ⇒ `Generator`

Infinite generator for all possible "numbers" from a set of digits.

Kind: static method of `G`
Returns: `Generator` - yields all digits as an array

Param Type Description
arr `Array` | `String` The set of digits

### G.cartesian(...sets) ⇒ `Generator`

Generates the cartesian product of the sets.

Kind: static method of `G`
Returns: `Generator` - yields each product as an array

Param Type Description
...sets `Array` | `String` variable number of sets of n elements.

### G.shuffle(arr) ⇒ `Array`

Shuffles an array in place using the Fisher–Yates shuffle.

Kind: static method of `G`
Returns: `Array` - a random, unbiased perutation of arr

Param Type Description
arr `Array` A set of elements.

## Keywords

### install

`npm i generatorics`

1,947

1.1.0

MIT

github.com

github