Function composition

Rainer Deyke root at rainerdeyke.com
Wed Aug 2 18:49:22 EDT 2000


"Cees de Groot" <cg at gaia.cdg.acriter.nl> wrote in message
news:8ma4g2$t57$1 at gaia.cdg.acriter.nl...
> I was showing a Math PhD/Functional programming guy Python, and of course
he
> was interested in map, filter, etcetera. He talked about function
composition,
> and I did a quick:
>
> def compose(f, g): return lambda x: f(g(x))
>
> but that's of course only for single-argument functions. My
> advanced-Python-hacking seems to be getting rusty (too much Java coding,
sorry
> for that), but is there an easy way to extend this to any argument
signature?

My guess would be:

def compose(f, g):
  return lambda *x, **y: f(apply(g, x, y))

I have no idea if this actually works.


--
Rainer Deyke (root at rainerdeyke.com)
Shareware action/role-playing games      -      http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor





More information about the Python-list mailing list