indices-permutations
is a recursive reducer to all permutations with repetitions of multi dimensional array indices
Installation | Usage | Examples | License
Installation
With npm do
npm install indices-permutations
Usage
Use it as a callback for the Array.prototype.reduce() on an array of positive integers. It returns the result array of all indices permutated.
Note that initialValue argument must be provided, for instance it can be an empty array []
.
Both CommonJS and ES6 import syntax are supported.
import indicesPermutations from 'indicesPermutations'
const indicesPermutations = require('indices-permutations')
Examples
All code in the examples below is intended to be contained into a single file.
order 1
Reduce [n]
to [[0], [1], ..., [n-1]]
[5].reduce(indicesPermutations, []) // [[0], [1], [2], [3], [4]]
order 2
Reduce [m, n]
to [[0, 0], [0, 1], ... ,[m-1, n-1]]
[3, 3].reduce(indicesPermutations, []) // [[0, 0], [0, 1], [0, 2],
// [1, 0], [1, 1], [1, 2],
// [2, 0], [2, 1], [2, 2]]
order 3
Reduce [a1, a2, a3]
to [[0, 0, 0], ... ,[a1 - 1, a2 - 1, a3 - 1]]
[2, 2, 3].reduce(indicesPermutations, []) // [[0, 0, 0], [0, 0, 1], [0, 0, 2],
// [0, 1, 0], [0, 1, 1], [0, 1, 2],
// [1, 0, 0], [1, 0, 1], [1, 0, 2],
// [1, 1, 0], [1, 1, 1], [1, 1, 2]]