Closed michaelficarra closed 7 years ago
Would [1, [2]].flatten()
throw? Being able to indiscriminately mix arrays and non-arrays seems like a primary use case for flatten.
I support throwing in the "return a non-isConcatSpreadable value from the function I give to flatMap
" case, but not the "flattening an array containing some non-isConcatSpreadable values" case.
@bakkot I would reject any inconsistency between [].flatten()
and [].flatMap(x => x)
.
I would also absolutely expect flatMap
to be able to return both an array and a non-array.
I see flatten as doing its best effort to give you a flattened array, but if it's already flat it should do nothing and if it's only partially flat, it should flatten the inflated parts only. If it's a matter of weighing possible bug-finding-ness of incorrect flatten vs. utility of allowing .flatten to operate on flat or partially-flat arrays, I see the latter being useful and I'm not convinced the former is going to save much developer consternation.
I think this argues for flatMap handling non-array return values properly - I too find the correspondence important.
I say no to both errors.
Works for me.
I would prefer to receive an error when I accidentally
[0].flatten()
or if I accidentally return a non-isConcatSpreadable value from the function I give toflatMap
.