[Python-ideas] Add nullifier argument to functools.reduce?
Steven D'Aprano
steve at pearwood.info
Sat Aug 23 19:43:58 CEST 2014
On Sat, Aug 23, 2014 at 10:25:06AM -0700, David Mertz wrote:
> This "nullifier" is mathematically called an "absorbing element", but
> saying an "attractor" might be a little more general. I.e. think of a
> local optimization problem, where multiple local min/max points might
> occur. If you reach one, further iteration won't budge from that point,
> even if it's not the "global absorbing element."
Yes. Note that arbitrary systems may have more than one attractors,
including cycles (a -> b -> c -> a) and even "strange attractors" of
infinite complexity. It's probably too much to expect reduce to deal
with cycles, but a really general solution should at least deal with
multiple attractors.
> Given that one can easily write one's own three line wrapper
> 'reduce_with_attractor()' for this special semantics
I don't think you can. Although it's 3:30am here and it's past my bed
time, so perhaps I'm wrong. The problem is that the wrapper cannot see
the reduced value until reduce() returns, and we want to short-circuit
the call once the reduced value is the attractor.
Still, easy or not, I think the semantics are too specialised to
justify in the standard library, especially given that Guido doesn't
like reduce and it almost got removed. A better solution, I think, would
be to stick this reduce_with_attractor() in some third-party functional
tool library.
--
Steven
More information about the Python-ideas
mailing list