UNPKG

@extra-array/permutations

Version:

Lists all possible permutations.

58 lines (43 loc) 1.66 kB
Lists all possible permutations. [:package:](https://www.npmjs.com/package/@extra-array/permutations) [:smiley_cat:](https://github.com/orgs/nodef/packages?repo_name=extra-array) [:running:](https://npm.runkit.com/@extra-array/permutations) [:vhs:](https://asciinema.org/a/332961) [:moon:](https://www.npmjs.com/package/@extra-array/permutations.min) [:scroll:](https://unpkg.com/@extra-array/permutations/) [:newspaper:](https://nodef.github.io/extra-array/) [:blue_book:](https://github.com/nodef/extra-array/wiki/) > Similar: [permutation], [permutations], [hasPermutation]. > This is part of package [extra-array]. [extra-array]: https://www.npmjs.com/package/extra-array <br> ```javascript array.permutations(x, [n]); // x: an array // n: number of values (-1 => any) ``` ```javascript const array = require("extra-array"); [...array.permutations([1, 2])]; // [ [], [ 1 ], [ 2 ], [ 1, 2 ], [ 2, 1 ] ] [...array.permutations([1, 2, 3])]; // [ // [], [ 1 ], // [ 2 ], [ 3 ], // [ 1, 2 ], [ 1, 3 ], // [ 2, 1 ], [ 2, 3 ], // [ 3, 1 ], [ 3, 2 ], // [ 1, 2, 3 ], [ 1, 3, 2 ], // [ 2, 1, 3 ], [ 2, 3, 1 ], // [ 3, 1, 2 ], [ 3, 2, 1 ] // ] [...array.permutations([1, 2, 3], 2)]; // [ [ 1, 2 ], [ 1, 3 ], [ 2, 1 ], [ 2, 3 ], [ 3, 1 ], [ 3, 2 ] ] ``` <br> <br> ## References - [Data.List.permutations: Haskell](https://hackage.haskell.org/package/base-4.12.0.0/docs/Data-List.html#v:permutations) [permutation]: https://github.com/nodef/extra-array/wiki/permutation [permutations]: https://github.com/nodef/extra-array/wiki/permutations [hasPermutation]: https://github.com/nodef/extra-array/wiki/hasPermutation