callbag-cartesian-product

2.0.0 • Public • Published

callbag-cartesian-product

Callbag factory that converts any number of pullable source into a single pullable source that emits the Cartesian product of the sources. This is unlikely to work correctly for listenable sources.

This is just a very thin wrapper around callbag-flatten, whose README shows the basic pattern of calling map inside map, and then flattening. Callbags allow us to have nice memory efficiency with extreme flexibility and composability.

Installation

In a Node.js project, run

$ npm install --save callbag-cartesian-project

API and examples

cartesian([fortranOrder,] ...sources)

Optional boolean fortranOrder (defaults to true) switches between Fortran ordering (first source alternates the fastest) and C ordering (first source changes the slowest). Compare the default Fortran ordering:

const cartesian = require('callbag-cartesian-product');
const { pipe, fromIter, forEach } = require('callbag-basics'); // from `npm i callbag-basics`
pipe(
    cartesian(fromIter([ 0, 1, 2 ]), fromIter('rgb'), fromIter([ true, false ])),
    forEach(x => console.log(x)),
)
// [ 0, 'r', true ]
// [ 1, 'r', true ]
// [ 2, 'r', true ]
// [ 0, 'g', true ]
// [ 1, 'g', true ]
// [ 2, 'g', true ]
// [ 0, 'b', true ]
// [ 1, 'b', true ]
// [ 2, 'b', true ]
// [ 0, 'r', false ]
// [ 1, 'r', false ]
// [ 2, 'r', false ]
// [ 0, 'g', false ]
// [ 1, 'g', false ]
// [ 2, 'g', false ]
// [ 0, 'b', false ]
// [ 1, 'b', false ]
// [ 2, 'b', false ]

to C ordering:

pipe(
    cartesian(false, fromIter([ 0, 1, 2 ]), fromIter('rgb'), fromIter([ true, false ])),
    forEach(x => console.log(x)),
)
// [ 0, 'r', true ]
// [ 0, 'r', false ]
// [ 0, 'g', true ]
// [ 0, 'g', false ]
// [ 0, 'b', true ]
// [ 0, 'b', false ]
// [ 1, 'r', true ]
// [ 1, 'r', false ]
// [ 1, 'g', true ]
// [ 1, 'g', false ]
// [ 1, 'b', true ]
// [ 1, 'b', false ]
// [ 2, 'r', true ]
// [ 2, 'r', false ]
// [ 2, 'g', true ]
// [ 2, 'g', false ]
// [ 2, 'b', true ]
// [ 2, 'b', false ]

Background

Dependencies (2)

Dev Dependencies (2)

Package Sidebar

Install

npm i callbag-cartesian-product

Weekly Downloads

0

Version

2.0.0

License

Unlicense

Unpacked Size

9.54 kB

Total Files

6

Last publish

Collaborators

  • fasiha