luafun icon indicating copy to clipboard operation
luafun copied to clipboard

[Feature Request] Add Flatten function

Open alexmozaidze opened this issue 2 years ago • 1 comments

It would be very cool to have. I sometimes face problems where I have a table of tables like

{
  { "thing1", "thing2" },
  { "thing3", "thing4" },
  -- ...
}

And want to flatten this mess out using just one simple operation:

fun.flatten(mess)

And get this majestic data structure in the end:

{
  "thing1",
  "thing2",
  "thing3",
  "thing4",
  -- ...
}

I realize that I can simulate flatten with reduce and chain, but it's far less intuitive

fun.reduce(function(acc, x) return fun.chain(acc, x) end, {}, mess)

in Fennel it's a bit nicer, but still noisy

(fun.reduce #(fun.chain $1 $2) [] mess)

alexmozaidze avatar Oct 25 '23 12:10 alexmozaidze

Using reduce also seems to result in a table instead of another iterator. I'd expect flatten() to produce an iterator

svermeulen avatar Dec 15 '23 08:12 svermeulen