montagejs / collections

This package contains JavaScript implementations of common data structures with idiomatic interfaces.
http://www.collectionsjs.com
Other
2.1k stars 183 forks source link

Implement deepFlatten such that [[[1], 2], 3].deepFlatten().equals([1, 2, 3]) #74

Open NV opened 10 years ago

kriskowal commented 10 years ago

Where work means return [1, 2], treating 1 as [1] since, since it is not Array.isArray? Array.isArray would be trouble because [Set([1]), Set([2])] should return [1, 2], not [[Set(1)], [Set([2])]]. Perhaps a duck test instead. I’ll try to stifle my impulse to drop the issue from the ivory tower of type purity :wink:

NV commented 10 years ago

Yes, the expected output is [1, 2].

kriskowal commented 10 years ago

@NV Do you expect flatten to be recursive/transitive deepFlatten, as in [[[1, 2], 3, [[[4]]]]] to be [1, 2, 3, 4]?

NV commented 10 years ago

I originally expected it to be recursive/transitive deepFlatten, but now I’m thinking there might be a use case for shallow flatten as well.

kriskowal commented 10 years ago

Shallow is certainly the behavior of the corresponding FRB operator. There are cases like concatenating map entries, [[key, value], [key, value]] or concatenating tables that would not work out.

kriskowal commented 10 years ago

I could add deepFlatten. I think the existence of an iterate method would suffice in distinguishing iterables from non-flattenable values.