Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #53260

Re: Is there a function that applies list of functions to a value?

Newsgroups comp.lang.python
Date 2013-08-29 14:48 -0700
References <aa168508-c469-4b02-9dc1-e0efd023d52b@googlegroups.com> <90afb50e-7932-4e05-a90d-7005e1de91b8@googlegroups.com> <mailman.373.1377810340.19984.python-list@python.org>
Message-ID <4544ac21-e36f-4d9e-8972-8f6a41e629ef@googlegroups.com> (permalink)
Subject Re: Is there a function that applies list of functions to a value?
From fp2161@gmail.com

Show all headers | View raw


On Thursday, August 29, 2013 11:05:38 PM UTC+2, Chris Angelico wrote:
> On Fri, Aug 30, 2013 at 6:50 AM,  <fp2161@gmail.com> wrote:
> 
> > My way is so obvious that it may not be that interesting...
> 
> >
> 
> > def func4(f1,f2,f3,f4):
> 
> >     def anon(x):
> 
> >         f1(f2(f3(f4(x))))
> 
> >     return anon
> 
> 
> 
> Or have it return the result of f1. And then, since it's an anonymous
> 
> function that simply returns an expression, I'd write it as:
> 
> 
> 
> def func4(f1,f2,f3,f4):
> 
>     return lambda x: f1(f2(f3(f4(x))))
> 
> 
> 
> Of course, that's still restricted to precisely four args. Extending
> 
> this concept to a variable number of arguments is, uhh, left as an
> 
> exercise to the reader. Which will probably end up going back to
> 
> reduce(). :)
> 
> 
> 
> ChrisA

Here is the generalisable version:


def comp(*func):
    def anon(x):
        res=x
        for f in func:
            res=f(res)
        return res
    return anon

you can then compose your function in __main__:
f4=comp(f5,f6,f7)
and call it:
print(x) ...

(I hope that Chris is happy now...)

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Is there a function that applies list of functions to a value? AdamKal <adamkalinski@gmail.com> - 2013-08-28 05:52 -0700
  Re: Is there a function that applies list of functions to a value? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-08-28 16:10 +0300
    Re: Is there a function that applies list of functions to a value? fp2161@gmail.com - 2013-08-29 15:48 -0700
  Re: Is there a function that applies list of functions to a value? Tim Chase <python.list@tim.thechases.com> - 2013-08-28 08:11 -0500
    Re: Is there a function that applies list of functions to a value? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-08-28 16:19 +0300
    Re: Is there a function that applies list of functions to a value? AdamKal <adamkalinski@gmail.com> - 2013-08-28 06:23 -0700
      Re: Is there a function that applies list of functions to a value? Chris Angelico <rosuav@gmail.com> - 2013-08-28 23:26 +1000
      Re: Is there a function that applies list of functions to a value? Tim Chase <python.list@tim.thechases.com> - 2013-08-28 08:43 -0500
        Re: Is there a function that applies list of functions to a value? AdamKal <adamkalinski@gmail.com> - 2013-08-28 06:50 -0700
  Re: Is there a function that applies list of functions to a value? ishish <ishish@domhain.de> - 2013-08-28 14:17 +0100
  Re: Is there a function that applies list of functions to a value? Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2013-08-28 15:09 +0200
  Re: Is there a function that applies list of functions to a value? Josh English <Joshua.R.English@gmail.com> - 2013-08-28 11:50 -0700
    Re: Is there a function that applies list of functions to a value? fp2161@gmail.com - 2013-08-29 23:17 -0700
      Re: Is there a function that applies list of functions to a value? alex23 <wuwei23@gmail.com> - 2013-08-30 16:36 +1000
        Re: Is there a function that applies list of functions to a value? Fabrice Pombet <fp2161@gmail.com> - 2013-08-30 01:11 -0700
  Re: Is there a function that applies list of functions to a value? fp2161@gmail.com - 2013-08-29 13:50 -0700
    Re: Is there a function that applies list of functions to a value? Chris Angelico <rosuav@gmail.com> - 2013-08-30 07:05 +1000
      Re: Is there a function that applies list of functions to a value? fp2161@gmail.com - 2013-08-29 14:27 -0700
        Re: Is there a function that applies list of functions to a value? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-30 02:18 +0000
        Re: Is there a function that applies list of functions to a value? Chris Angelico <rosuav@gmail.com> - 2013-08-30 07:35 +1000
          Re: Is there a function that applies list of functions to a value? fp2161@gmail.com - 2013-08-29 23:14 -0700
            Re: Is there a function that applies list of functions to a value? alex23 <wuwei23@gmail.com> - 2013-08-30 16:23 +1000
              Re: Is there a function that applies list of functions to a value? Fabrice Pombet <fp2161@gmail.com> - 2013-08-30 01:06 -0700
      Re: Is there a function that applies list of functions to a value? fp2161@gmail.com - 2013-08-29 14:48 -0700
        Re: Is there a function that applies list of functions to a value? Terry Reedy <tjreedy@udel.edu> - 2013-08-30 00:38 -0400
    Re: Is there a function that applies list of functions to a value? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-30 02:09 +0000
      Re: Is there a function that applies list of functions to a value? fp2161@gmail.com - 2013-08-29 23:06 -0700

csiph-web