[Edu-sig] composing functions (was Great Computer Shootout)
Kirby Urner
pdx4d@teleport.com
Sun, 03 Jun 2001 10:25:08 -0700
>Drawing a blank this AM re how to do this in Python, trying
>various things with little lambda.
>
>Clues?
>
>Kirby
If I write in a module, I can do it like this:
Module: proto2.py
======
from __future__ import nested_scopes
def compose(f,g):
return lambda x: f(g(x))
======
>>> def f(x): return x*x
>>> def g(x): return x+2
>>> from proto2 import compose
>>> h = compose(f,g)
>>> h
<function <lambda> at 0123593C>
>>> h(10)
144
However, I can't get a similar compose to work interactively be
entering the function in IDLE, even if I import nested_scopes.
This relates to an earlier post, where I was having difficulty
getting the nested feature to work in top-level IDLE.0.8.
Kirby