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: 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: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <7867a7cb-e419-4f2e-8d3d-30862de93014@googlegroups.com> 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 On Wednesday, August 28, 2013 8:50:53 PM UTC+2, Josh English wrote: > Reduce tricks are nice, but I prefer clarity sometimes: >=20 >=20 >=20 > def double(x): >=20 > return x*2 >=20 >=20 >=20 > def add3(x): >=20 > return x+3 >=20 >=20 >=20 >=20 >=20 > def compose(*funcs): >=20 > for func in funcs: >=20 > if not callable(func): >=20 > raise ValueError('Must pass callable functions') >=20 >=20 >=20 > def inner(value): >=20 > for func in funcs: >=20 > value =3D func(value) >=20 > return value >=20 >=20 >=20 > return inner >=20 >=20 >=20 >=20 >=20 > add_then_double =3D compose(add3, double) >=20 > double_then_add =3D compose(double, add3) >=20 >=20 >=20 > print add_then_double(1) # prints 8 >=20 > 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 t= his context, it would raise an Error anyway.