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


Groups > comp.lang.python > #53279

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

X-Received by 10.224.52.6 with SMTP id f6mr8395640qag.2.1377843439489; Thu, 29 Aug 2013 23:17:19 -0700 (PDT)
X-Received by 10.49.99.65 with SMTP id eo1mr280699qeb.3.1377843439466; Thu, 29 Aug 2013 23:17:19 -0700 (PDT)
Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!fx3no6151302qab.0!news-out.google.com!he10ni3964qab.0!nntp.google.com!fx3no6151295qab.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.python
Date Thu, 29 Aug 2013 23:17:19 -0700 (PDT)
In-Reply-To <cf0fa304-1965-4173-89cd-6167a3427439@googlegroups.com>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=81.64.227.8; posting-account=GXMXogoAAACf5vrPm48pFHKk2Gwl-I0g
NNTP-Posting-Host 81.64.227.8
References <aa168508-c469-4b02-9dc1-e0efd023d52b@googlegroups.com> <cf0fa304-1965-4173-89cd-6167a3427439@googlegroups.com>
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <7867a7cb-e419-4f2e-8d3d-30862de93014@googlegroups.com> (permalink)
Subject Re: Is there a function that applies list of functions to a value?
From fp2161@gmail.com
Injection-Date Fri, 30 Aug 2013 06:17:19 +0000
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
X-Received-Bytes 2364
Xref csiph.com comp.lang.python:53279

Show key headers only | View raw


On Wednesday, August 28, 2013 8:50:53 PM UTC+2, Josh English wrote:
> Reduce tricks are nice, but I prefer clarity sometimes:
> 
> 
> 
> def double(x):
> 
>     return x*2
> 
> 
> 
> def add3(x):
> 
>     return x+3
> 
> 
> 
> 
> 
> def compose(*funcs):
> 
>     for func in funcs:
> 
>         if not callable(func):
> 
>             raise ValueError('Must pass callable functions')
> 
> 
> 
>     def inner(value):
> 
>         for func in funcs:
> 
>             value = func(value)
> 
>         return value
> 
> 
> 
>     return inner
> 
> 
> 
> 
> 
> add_then_double = compose(add3, double)
> 
> double_then_add = compose(double, add3)
> 
> 
> 
> print add_then_double(1) # prints 8
> 
> print double_then_add(1) # prints 5


This is my favourite design, simple, clear, straightforward, very pythonic imho.  So great that I actually dod not notice it, and wrote it again after you! Imho still, the ValueError you are raising is not that important in this context, it would raise an Error anyway.

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