Skip to content

flatMap

Subhajit Sahu edited this page Dec 28, 2022 · 14 revisions

Flatten nested ilists, based on map function.

Alternatives: flat, flatMap.


function flatMap(x, fm, ft)
// x:  nested ilists
// fm: map function (v, k, x)
// ft: test function for flatten (v, k, x) [is]
const xilists = require('extra-ilists');

var x = [['ab', 'cde'], [
  [['a', 'b'], [1, 2]],
  [['c', 'de'], [
    3,
    [['d', 'e'], [
      3,
      [['e'], [
        5
      ]]
    ]]
  ]]
]];
xilists.flatMap(x).map(c => [...c]);
// → [ [ 'a', 'b', 'c', 'de' ], [ 1, 2, 3, [ [Array], [Array] ] ] ]

xilists.flatMap(x, v => xilists.flat(v, 1)).map(c => [...c]);
// → [ [ 'a', 'b', 'c', 'd', 'e' ], [ 1, 2, 3, 3, [ [Array], [Array] ] ] ]

xilists.flatMap(x, v => xilists.flat(v)).map(c => [...c]);
// → [ [ 'a', 'b', 'c', 'd', 'e' ], [ 1, 2, 3, 3, 5 ] ]


References

Clone this wiki locally