Function composition

tanzer at swing.co.at tanzer at swing.co.at
Thu Aug 3 02:18:47 EDT 2000


cg at gaia.cdg.acriter.nl (Cees de Groot) wrote:

> 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?

For g, there is:

class Composition :

    def __init__ (self, outer, inner) :
        self.outer = outer
        self.inner = inner
    # end def __init__
    
    def __call__ (self, * args, ** kw) :
        return self.outer (apply (self.inner, args, kw))
    # end def __call__
    
# end class Composition


-- 
Christian Tanzer                                         tanzer at swing.co.at
Glasauergasse 32                                       Tel: +43 1 876 62 36
A-1130 Vienna, Austria                                 Fax: +43 1 877 66 92





More information about the Python-list mailing list