Re:How to stop functional programming
by mopierotti on Tuesday February 23, 2016 @02:30PM
(#11161604)
Hmm I would expect the results to be based on the data structure holding the values you're flattening. An in-order depth first traversal if the data structure you're applying it to has an inherent order (e.g a List) and in an undefined order if it's something unordered like a Set.
[ Parent
| on HN ]
I would agree, but have a look at this:
http://reactivex.io/documentation/operators/flatmap.html
Granted, this refers to a higher level abstraction on a sequence of realtime events, but as we delve more into green threads and lazy evaluation, these kinds of solutions will become more common.
I really don't believe that there is no obvious way to guarantee the ordering of a given flatten command without looking into its implementation. Of course, I'm admittedly partial to Python's solution - use an explicit list comprehension:
>>> g = lambda i: (i, -i)
>>> [x for y in [1, 2, 3] for x in g(y)]
[1, -1, 2, -2, 3, -3]