You know how flatmap takes a sequence of items and converts each one into a new subsequence, aggregating all the subsequences:
[A, B, C] -> [A1, A2, B1, B2, B3, C1]
Is there a name for the transform which does the opposite? Something like:
[A1, A2, B1, B2, B3, C1] -> [A, B, C]
The specific example that got me thinking about this was doing evaluation of mathematical expressions:
1 * 2 + 3 * 4 + 5 + 6 * 7 * 8
-> 2 + 12 + 5 + 6 + 336
-> 361
Individually, the evaluation of the 6 * 7 * 8
seems like a classic reduce
step, while deciding which blocks need to be reduced would need repeated takeWhile
steps.
I know how to do this in the classic iterative way, keeping track of indices and all that. For most cases, I've found a nice functional replacement for most iterative patterns. Is there a name for a single operation that does this, or a simple set of operations which can be composed to create this effect?
I think the opposite of flatmap
is groupby
.
$ python3
>>> from itertools import groupby
>>> groupby(['A1', 'A2', 'B1', 'B2', 'B3', 'C1'], lambda x: x[0])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With