xprod calculates the cross product of arrays.
Category | Status |
---|---|
Version | |
Dependencies | |
Dev dependencies | |
Build | |
License |
$ npm install xprod
First you need to add a reference to xprod to your application:
const { xprod } = require('xprod');
If you use TypeScript, use the following code instead:
import { xprod } from 'xprod';
To calculate the cross product of two or more arrays, call the xprod function and provide the arrays as parameter. The cross product is returned as a generator to allow multiplying arbitrarily large arrays:
const result = xprod([
[ 'linux', 'windows', 'macOS' ],
[ 'node-10', 'node-12' ]
]);
// Convert the generator to an array for logging.
console.log([...result]);
// => [
// [ 'linux', 'node-10' ],
// [ 'linux', 'node-12' ],
// [ 'windows', 'node-10' ],
// [ 'windows', 'node-12' ],
// [ 'macOS', 'node-10' ],
// [ 'macOS', 'node-12' ],
// ]
Since the cross product is calculated lazily, this works, too:
const result = xprod([
new Array(100000).fill('foo'),
new Array(100000).fill('bar')
]);
Keep in mind that empty arrays in the input will result in an empty output:
const result = xprod([
new Array(100000).fill('foo'),
[]
]);
// Convert the generator to an array for logging.
console.log([...result]);
// => []
To run quality assurance for this module use roboter:
$ npx roboter