Ruby and Python

gbreed at cix.compulink.co.uk gbreed at cix.compulink.co.uk
Tue Nov 21 09:17:52 EST 2000


Gareth McCaughan wrote:

> Here's one criterion. It's not a serious attempt at a
> rigorous definition; just a handy indicator. "Is it
> possible to write a function that takes two functions
> of one argument each and returns the composition of
> those functions?".
> 
> In Python, you can do this.
> 
>   def compose(f,g):
>     def _(x,g=g): return f(g(x))
>     return _

Almost.  You can do this:

>>> def compose(f,g):
...   def temp(x, f=f, g=g):
...     return f(g(x))
...   return temp
...
>>> compose(len, str)(300)
3


or even this:

>>> def compose(f,g):
...   return lambda x, f=f, g=g: f(g(x))
...
>>> compose(len, str)(20)
2


       Graham B



More information about the Python-list mailing list