New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add foldr and foldl to functional module #42810
Comments
This patch adds foldr and foldl functions to the The patch is against svn revision 42097. |
Logged In: YES What does this add over the (currently builtin) reduce? reduce(func, iter, initial) and reduce(func, reversed(iter), initial) Is it just that foldr and foldl are more modern names? Maybe even show how to create foldr and foldl as an example, |
Logged In: YES I was under the impression that reduce was going to be In any case, while reduce() is indeed another name for """
>>> def sub(a, b): return a - b
...
>>> foldr(sub, 0, [1, 2, 3])
2
>>> reduce(sub, [3, 2, 1], 0)
-6
""" I'd be happy to update the patch to include references to |
Logged In: YES Guido thinks that reduce is too hard to understand, because (1) Should the functional module also mention other (2) Could you show the step-by-step for foldr? I missed At this point, my best guess is that
while so foldr(f, zero, [x1, x2, x3...xn])
<==>
f(x1, f(x2, f(x3, ... f(xn, zero))))
while
foldl(f, zero, [x1, x2, x3...xn])
<==>
f(f(f(f(f(zero, x1), x2), x3) ... ), xn) but I'm still not *sure* I got it right. |
Logged In: YES Ah, I had thought reduce was going away sooner than Python 3.0. I've updated the patch to include an expansion of both foldl |
Logged In: YES In any case, having those functions in functional, under |
Logged In: YES Review: I recommend to apply this patch. It includes test cases and documentation. Cannot break backwards compatibility, as the entire module The added functions are reasonably standard for modern |
Logged In: YES I found a reference leak in my implementation of foldr. Accordingly, I'm closing this current patch. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: