|
|
|
|
|
by chronial
1307 days ago
|
|
I can only quote OP on this: > But if you feel the need to transform simple linear code into a map of higher order functions because of some abstract benefits you're probably doing stuff that's too easy for you and you should get your mental challenge from solving a more difficult problem instead. This code is only complicated because you insist on following some abstract ideal.
The actual way to solve this in python is: total_foos = sum(elem.foo for elem in elems)
pruned = [e for e in elems if e.foo < len(elems)]
Which is shorten than even your first code sample. If you directly translate your last example into sensible python, you get a nice example of some "simple linear code": total_foos = 0
pruned = []
for elem in elems:
total_foos += elem.foo
if elem.foo < len(elems):
pruned.append(elem)
The existence of statements in python clearly stands in the way of achieving ideals of pure functional programming. But I think aiming for such ideals is the exact opposite of the point OP was making. |
|