Giter Club home page Giter Club logo

indices-permutations's Introduction

indices-permutations

is a recursive reducer to all permutations with repetitions of multi dimensional array indices

Installation | Usage | Examples | License

NPM version Build Status No deps JavaScript Style Guide

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]]

License

MIT

indices-permutations's People

Contributors

fibo avatar

Watchers

 avatar  avatar  avatar

indices-permutations's Issues

Avoid unnecessary for loops

Few improvements are possible: arrayWithoutLastElement could be achieved with a slice or an ushift, currentValue is not used at all and could avoid some computation about previous iterations.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.