Functional Programming

Mike Meyer mwm at mired.org
Sun Dec 29 22:51:24 EST 2002


"Martin v. Löwis" <martin at v.loewis.de> writes:

> beno wrote:
> > Does anyone know of good sources of info for programming using the
> > principles of functional programming in Python? Or, to what
> > languages is Haskell similar? Any other advice on the subject
> > equally welcomed!
> - Currying (creating a function from another function by passing some
>    arguments): This is not directly available in Python; you can often
>    emulate it with lambda expressions.

You don't need lambdas, and you can even make the programmer API look
the same. For example:

>>> inc = curry(int.__add__, 1)
>>> inc(3)
4
>>> inc(10)
11

The definition of curry is:

class curry:
   def __init__(self, func, *fixed_args):
      self.func = func
      self.fixed_args = fixed_args

   def __call__(self, *variable_args):
      return apply(self.func, self.fixed_args + variable_args)

This idiom is where my python enlightenment began.

        <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list