Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

array-permutation

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

array-permutation

permutation utils

  • 0.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.7K
increased by74.79%
Maintainers
1
Weekly downloads
 
Created
Source

array-permutation

permutation utils. Return Iterable for iterating all permutations of an array.

Usage

var permutation = require('array-permutation');
var range = permutation.range;
var random = permutation.random;
var shuffle = permutation.shuffle;

range(n)

Return [0,..,n-1]

range(low, high, step)

Return [low, low + step, low + 2 * step,..., low + k * step], with low + k * step < high

random(n)

Return an array populated with a random permutation of range(n)

random(low, high, step)

Return an array populated with a random permutation of range(low, high, step)

permutation(n)

Return an iterable, for iterating all permutations of range(n)

permutation(low, high, step)

Return an iterable, for iterating all permutations of range(low, high, step)

permutation([])

Return an iterable, for iterating all permutations of the given array.

shuffle([])

Shuffle the given array, and return it.

Example

permutation

var perm = require('..');
var iter = perm([1, 2, 3, 4]);
for (var p of iter) {
    console.log(p);
}

output:

⌘ node example/permutation.js
[ 1, 2, 3, 4 ]
[ 2, 1, 3, 4 ]
[ 2, 3, 1, 4 ]
[ 2, 3, 4, 1 ]
[ 1, 3, 2, 4 ]
[ 3, 1, 2, 4 ]
[ 3, 2, 1, 4 ]
[ 3, 2, 4, 1 ]
[ 1, 3, 4, 2 ]
[ 3, 1, 4, 2 ]
[ 3, 4, 1, 2 ]
[ 3, 4, 2, 1 ]
[ 1, 2, 4, 3 ]
[ 2, 1, 4, 3 ]
[ 2, 4, 1, 3 ]
[ 2, 4, 3, 1 ]
[ 1, 4, 2, 3 ]
[ 4, 1, 2, 3 ]
[ 4, 2, 1, 3 ]
[ 4, 2, 3, 1 ]
[ 1, 4, 3, 2 ]
[ 4, 1, 3, 2 ]
[ 4, 3, 1, 2 ]
[ 4, 3, 2, 1 ]

random

var random = require('..').random;

console.log(random(5));                 // [ 3, 2, 4, 1, 0  ]
console.log(random(2, 5));              // [ 4, 3, 2  ]
console.log(random(1, 5, 2));           // [ 3, 1  ]
console.log(random([1, 2, 3, 4, 5]));   // [ 4, 5, 3, 1, 2  ]

output:

⌘ node example/random.js
[ 4, 0, 1, 2, 3 ]
[ 4, 2, 3 ]
[ 3, 1 ]
[ 3, 4, 1, 5, 2 ]

Keywords

FAQs

Package last updated on 30 May 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc