Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fasiha/callbag-cartesian-product
Callbag factory that yields the Cartesian product of source callbags 👜
https://github.com/fasiha/callbag-cartesian-product
Last synced: 15 days ago
JSON representation
Callbag factory that yields the Cartesian product of source callbags 👜
- Host: GitHub
- URL: https://github.com/fasiha/callbag-cartesian-product
- Owner: fasiha
- License: unlicense
- Created: 2018-03-13T08:54:49.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-04-23T19:49:42.000Z (over 1 year ago)
- Last Synced: 2024-09-15T22:10:47.939Z (about 2 months ago)
- Language: JavaScript
- Homepage:
- Size: 28.3 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-callbags - cartesian-product
README
# 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`](https://github.com/staltz/callbag-flatten), whose README shows the basic pattern of calling `map` inside `map`, and then `flatten`ing. Callbags allow us to have nice memory efficiency with extreme flexibility and composability.
Recursively creating Cartesian callbags from Cartesian callbags, using this simple library, is not very ergonomic—you might get unexpected nesting. In those situations, just grab @avinashcodes' [`callbag-flat-map`](https://github.com/avinashcodes/callbag-flat-map), which gives you full control over combining Cartesian callbags with other callbags.
## 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:
```js
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:
```js
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
- [`callbag-basics`](https://github.com/staltz/callbag-basics) and links to articles therein
- [GitHub's search results for "callbag"](https://github.com/search?q=callbag&type=Repositories&utf8=%E2%9C%93)
- André Staltz's ["Why we need callbags"](https://staltz.com/why-we-need-callbags.html)