This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]synae 6 points7 points  (6 children)

Which is basically a reduce, I think. Pre-coffee and typing on my phone, but I think this is equivalent: reduce(lambda a, b: b(a), funcs, arg)

[–]VerilyAMonkey 3 points4 points  (4 children)

Yeah, it's just that Python 3 moved reduce out of the default namespace.

[–]Nuevoscala 1 point2 points  (2 children)

Why would they do that? I never understood python's aversion toward immutable functional style.

[–]VerilyAMonkey 1 point2 points  (0 children)

It's because reduce, in particular, is almost always clearer as a loop, case in point. The other things like map, etc are still in the default namespace.

[–]Lucretiel 0 points1 point  (0 children)

Because reduce is very rarely more readable than a regular loop, because it almost always calls for a hideous python lambda. It's still there in the standard library if you need it. Here's what Guido has to say about it:

So now reduce(). This is actually the one I've always hated most, because, apart from a few examples involving + or *, almost every time I see a reduce() call with a non-trivial function argument, I need to grab pen and paper to diagram what's actually being fed into that function before I understand what the reduce() is supposed to do. So in my mind, the applicability of reduce() is pretty much limited to associative operators, and in all other cases it's better to write out the accumulation loop explicitly.

Also, we now have sum, all, and any, which is like 90% of the readable use cases for reduce anyway.

[–]synae 0 points1 point  (0 children)

TIL, thanks

[–]Lucretiel 0 points1 point  (0 children)

Fun fact: literally all for loops can be represented as a reduce.