tc39 / proposal-flatMap

proposal for flatten and flatMap on arrays
https://tc39.github.io/proposal-flatMap
214 stars 19 forks source link

throw when attempting to flatten a value which is not isConcatSpreadable #27

Closed michaelficarra closed 7 years ago

michaelficarra commented 7 years ago

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 to flatMap.

ljharb commented 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.

bakkot commented 7 years ago

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.

michaelficarra commented 7 years ago

@bakkot I would reject any inconsistency between [].flatten() and [].flatMap(x => x).

ljharb commented 7 years ago

I would also absolutely expect flatMap to be able to return both an array and a non-array.

bterlson commented 7 years ago

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.

michaelficarra commented 7 years ago

Works for me.