combinations
Generate all possible combinations, which are all the unordered samples of size k, without replacement, from a set of n objects. The number of k-combinations is equal to the binomial coefficient:
Very low memory footprint even if the number of combinations to generate is high. Thank to generators, you can iterate over all possible samples, without creating a very large array.
Installation
$ npm install ml-combinations
Usage
// the package exports a generator functionconst combinations = ;const options = mode: 'index' ; // the generator function returns an iteratorvar gen = ; // You can loop throw the iteratorfor let combination of gen console; // Or use destructuring, if you want to manipulate the array with all possible sample combinationsconsole; // [ [ 3, 2 ], [ 0, 2 ], [ 1, 2 ], [ 1, 2 ], [ 0, 2 ], [ 0, 1 ] ] // Use mask mode instead of index mode (index mode is the default)optionsmode = 'mask';gen = ;console; // [ [ 0, 0, 1, 1 ][ 1, 0, 0, 1 ],[ 0, 1, 0, 1 ],[ 0, 1, 1, 0 ],[ 1, 0, 1, 0 ],[ 1, 1, 0, 0 ] ]
References
Phillip J Chase, `Algorithm 382: Combinations of M out of N Objects [G6]', Communications of the Association for Computing Machinery 13:6:368 (1970). To the article