JSPM

powerset-gen

0.1.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q26663F
  • License MIT

A generator function that produces all subsets of the given set in input

Package Exports

  • powerset-gen

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (powerset-gen) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

powerset-gen

A generator function that produces all subsets of the given set in input.

Install

npm install powerset-gen

Usage

Both ES6 Module and CommonJS distributions are provided by this package.

ES6 Module

import generator from "poweset-get"

ES5 / CommonJS

var generator = require("powerset-gen");

API

The module exports the following function.

generator(set, [mapper])Generator

Param Type Default Description
set Array The given set in input
mapper Function (value, index) => value (optional) Function that maps the yielded value, taking two arguments:
- value The current subset being generated
- index The index of the current subset being generated

Examples

for..of

for (const subset of generator(["a", "b", "c"])) {
  console.log(subset)
}

Output

[]
["a"]
["b"]
["a", "b"]
["c"]
["a", "c"]
["b", "c"]
["a", "b", "c"]

Generate an Array

[...generator(["a", "b"])]
// or
Array.from(generator(["a", "b"]))

Generate a Set

new Set(generator(["a", "b"]))

Spreading in function call

console.log(...generator(["a", "b"]))

Indexes

for (const [index, subset] of generator(["a", "b", "c"], (value, i) => [i, value])) {
  console.log(index, subset)
}

Output

0 []
1 ["a"]
2 ["b"]
3 ["a", "b"]
4 ["c"]
5 ["a", "c"]
6 ["b", "c"]
7 ["a", "b", "c"]

Binary mask

for (const [mask, subset] of generator(["a", "b", "c"], (value, index) => [index.toString(2), value])) {
  console.log(mask, subset)
}

Output

0 []
1 ["a"]
10 ["b"]
11 ["a", "b"]
100 ["c"]
101 ["a", "c"]
110 ["b", "c"]
111 ["a", "b", "c"]