by rspencer · 1y ago · 0 comments
max(map(sum, input_list.split(None)))
To decipher this the eye has to jump to the middle of the line, move rightwards, then to the left to see the "map" then move right again to see what we are mapping and then all the way to the beginning to find the "max".The author would probably suggest rust's syntax* of
values.iter().split(None).map(Iterator::sum).max().unwrap_or(0)
but I was learning q at the time so came up with the much clearer /s, right to left max((0^+)\)l
*: Though neither python nor rust have such a nice `.split(None)` built in.Sorry, I'm not sure I understand what `.split(None)` would do? My initial instinct is that would would return each character. i.e. `.chars()` in Rust or `list(s)` in Python.
Reading the docs [0] it seems `.split(None)` returns an array of the indivual characters without whitespace - so something like [c in list(s) if not whitespace(c)]
[0] https://docs.python.org/3.3/library/stdtypes.html?highlight=...
Say what you will about clarity, but my mind sort of glossed over the intention in the python and rust code, focusing instead on the syntax, while the q code made me consider what was actually happening.
Perhaps q did both force you to consider what was happening, and hide it from you...
Though hopefully, once the idiom is learned, I'll be able to remember it :)
Curious as to why a scan \ rather than a reduction /?
This may eventually be upstreamed: https://github.com/rust-itertools/itertools/issues/1026
input_groups = input_list.split(None)
group_sums = map(sum, input_groups)
max_sum = max(group_sums)
This also gives you a slightly higher-level view on how the algorithm proceeds just by reading the variable names on the LHS.I prefer python comprehensions for the same reason. I can tell that they are about crating a list or a dict and I don't need to parse a bunch of operations in a loop to come to the same conclusion.