function operators

Kragen Sitaker kragen at pobox.com
Tue Nov 27 14:19:07 EST 2001


"James A. H. Skillen" <jahs at jahs.org> writes:

> Has anyone ever wished that Python had operators defined on functions?
> For example, suppose you want the function f(x) = cos(x) + sin(x).
> ...
> but wouldn't:
>     f = cos + sin
> be *much* nicer?
> ...
> Taking the idea of PEP 211 further, we could have the "outer product"
> operator "@" work on functions too.

You want something like the Functor class from functional.py in the
xoltar toolkit; it overloads * to mean composition, ~, &, and | to do
the appropriate shortcut logical operations, + to mean sequence, and
<< and >> to do currying.  See
http://sourceforge.net/projects/xoltar-toolkit for more. It's pretty
nifty, probably the coolest code I found in the Vaults.

Last year sometime, I actually wrote a bunch of code to do algebraic
manipulations, and I overloaded all kinds of Python operators to
create expressions.  Can't remember if I ever posted it; if not, it's
gone now, anyway.




More information about the Python-list mailing list