Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #53149 > unrolled thread
| Started by | AdamKal <adamkalinski@gmail.com> |
|---|---|
| First post | 2013-08-28 05:52 -0700 |
| Last post | 2013-08-29 23:06 -0700 |
| Articles | 7 on this page of 27 — 12 participants |
Back to article view | Back to comp.lang.python
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
Page 2 of 2 — ← Prev page 1 [2]
| From | fp2161@gmail.com |
|---|---|
| Date | 2013-08-29 23:14 -0700 |
| Message-ID | <50764540-6edd-4d65-b44c-9fbd1daf9043@googlegroups.com> |
| In reply to | #53272 |
On Thursday, August 29, 2013 11:35:39 PM UTC+2, Chris Angelico wrote: > On Fri, Aug 30, 2013 at 7:27 AM, <fp2161@gmail.com> wrote: > > > Chris, call me a snob, but I resent using lambdas (aren't they usually considered odd/bad practice in python?) > > > > They're not bad practice; all they are is a function without a name, > > that's restricted to returning a single expression. So they're > > perfectly suited to this task, and ill-suited to some others. > > > > Like everything, lambda's a tool that can be used or misused. > > > > ChrisA For this purpose however, I suspect that a named function with a proper docstring that can be imported and reused over and over again is probably more appropriate than a lambda (the first post is telling us about something happening from time to time...)
[toc] | [prev] | [next] | [standalone]
| From | alex23 <wuwei23@gmail.com> |
|---|---|
| Date | 2013-08-30 16:23 +1000 |
| Message-ID | <kvpdpi$87t$1@dont-email.me> |
| In reply to | #53278 |
On 30/08/2013 4:14 PM, fp2161@gmail.com wrote: > For this purpose however, I suspect that a named function with a proper docstring that can be imported and reused over and over again is probably more appropriate than a lambda Given that in Chris' example the lambda was returned from a factory, importing the inner function by name is never going to be a concern. It's also possible to assign to both __name__ and __doc__ for a lambda, which can be useful at times.
[toc] | [prev] | [next] | [standalone]
| From | Fabrice Pombet <fp2161@gmail.com> |
|---|---|
| Date | 2013-08-30 01:06 -0700 |
| Message-ID | <06f5bce9-bfae-4700-b81e-195e84e54454@googlegroups.com> |
| In reply to | #53280 |
On Friday, August 30, 2013 8:23:44 AM UTC+2, alex23 wrote: > On 30/08/2013 4:14 PM, fp2161@gmail.com wrote: > > > For this purpose however, I suspect that a named function with a proper docstring that can be imported and reused over and over again is probably more appropriate than a lambda > > > > Given that in Chris' example the lambda was returned from a factory, > > importing the inner function by name is never going to be a concern. > > > > It's also possible to assign to both __name__ and __doc__ for a lambda, > > which can be useful at times. I am sorry, I can't see any of Chris' code in this thread, where is it???
[toc] | [prev] | [next] | [standalone]
| From | fp2161@gmail.com |
|---|---|
| Date | 2013-08-29 14:48 -0700 |
| Message-ID | <4544ac21-e36f-4d9e-8972-8f6a41e629ef@googlegroups.com> |
| In reply to | #53258 |
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...)
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2013-08-30 00:38 -0400 |
| Message-ID | <mailman.379.1377837555.19984.python-list@python.org> |
| In reply to | #53260 |
On 8/29/2013 5:48 PM, fp2161@gmail.com wrote:
> Here is the generalisable version:
>
> def comp(*func):
> def anon(x):
> res=x
> for f in func:
> res=f(res)
> return res
> return anon
With a bit more work, one can set the __name__ and __qualname__ attributes.
import math as m
def comp(*func, name=''):
def anon(x):
res=x
for f in func:
res=f(res)
return res
if name:
anon.__name__ = name
q = anon.__qualname__.rsplit('.', maxsplit=1)
q[1] = name
anon.__qualname__ = '.'.join(q)
return anon
esincos = comp(m.exp, m.sin, m.cos, name='esincos')
print(esincos)
#
<function comp.<locals>.esincos at 0x00000000033107B8>
--
Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-08-30 02:09 +0000 |
| Message-ID | <521ffee9$0$6599$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #53255 |
On Thu, 29 Aug 2013 13:50:39 -0700, fp2161 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 I don't think "obvious" is quite the right description. Well, perhaps "obviously wrong" :-) You also need to define func1 (trivial), func2, func3, func5, func6, func7, func8, ..., func2147483647, plus another master function to choose between them, depending on the number of functions provided as argument. I assume that the maximum number of arguments given is 2**31-1. Python may not actually have that limitation, in which case you would need to define additional functions. Or... you would have to come up with an implementation which doesn't hard- code the number of functions used. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | fp2161@gmail.com |
|---|---|
| Date | 2013-08-29 23:06 -0700 |
| Message-ID | <cb951743-7dad-4939-975c-15913c9c572e@googlegroups.com> |
| In reply to | #53268 |
On Friday, August 30, 2013 4:09:45 AM UTC+2, Steven D'Aprano wrote: > On Thu, 29 Aug 2013 13:50:39 -0700, fp2161 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 > I don't think "obvious" is quite the right description. Well, perhaps > > "obviously wrong" :-) > You also need to define func1 (trivial), func2, func3, func5, func6, > > func7, func8, ..., func2147483647, plus another master function to choose > > between them, depending on the number of functions provided as argument. > I assume that the maximum number of arguments given is 2**31-1. Python > > may not actually have that limitation, in which case you would need to > > define additional functions. > Or... you would have to come up with an implementation which doesn't hard- > > code the number of functions used. > > Steven I got the generalisation criticism before yours, and generalised it accordingly. Unfortunately it was wrong essentially because it was so obvious that Josh Englsih posted essentially the same one before me...
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.python
csiph-web