Skip to content
Subhajit Sahu edited this page Dec 2, 2022 · 16 revisions

Flatten nested set to given depth.

Alternatives: flat, flatMap.


function flat(x, n, fm, ft)
// x:  a nested set
// n:  maximum depth [-1 ⇒ all]
// fm: map function (v, v, x)
// ft: flatten test function (v, v, x) [is]
const set = require('extra-set');

var x = new Set([
  new Set([1, 2]),
  new Set([3, new Set([4, new Set([5])])])
]);
set.flat(x);
// → Set(5) { 1, 2, 3, 4, 5 }

set.flat(x, 1);
// → Set(4) { 1, 2, 3, Set(2) { 4, Set(1) { 5 } } }

set.flat(x, 2);
// → Set(5) { 1, 2, 3, 4, Set(1) { 5 } }


References

Clone this wiki locally