combinatorial-explosion
Boom.
Install it with:
npm install --save combinatorial-explosion
Usage
Import it in your favourite way:
import { explode, explodeTree } from 'combinatorial-explosion';
var combExpl = require('combinatorial-explosion');
var explode = combExpl.explode;
var explodeTree = combExpl.explodeTree;
then you can use it to blow up lists:
console.dir(explode([
[ 'a1', 'a2' ],
[ 'b' ],
[ 'c1', 'c2' ]
]));
[ [ 'a1', 'b', 'c1' ],
[ 'a1', 'b', 'c2' ],
[ 'a2', 'b', 'c1' ],
[ 'a2', 'b', 'c2' ] ]
and trees (procedurally):
const tree = {
id: 'a',
children: [
{ id: 'b', children: [] },
{ id: 'c', children: [] }
]
};
function fork(node) {
if (node.id === 'c') {
return [ { ...node, id: 'c1' }, { ...node, id: 'c2' } ];
}
else {
return [ node ];
}
}
function extract(node) {
return node.children;
}
function compose(node, children) {
return { ...node, children };
}
console.dir(explodeTree(tree, { fork, extract, compose }));
[
{
id: 'a',
children: [
{ id: 'b', children: [] },
{ id: 'c1', children: [] }
]
},
{
id: 'a',
children: [
{ id: 'b', children: [] },
{ id: 'c2', children: [] }
]
}
]
License
This library, combinatorial-explosion, is free software ("Licensed Software"); you can
redistribute it and/or modify it under the terms of the GNU Lesser General
Public License as published by the
Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; including but not limited to, the implied warranty of MERCHANTABILITY,
NONINFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if
not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
Floor, Boston, MA 02110-1301 USA