Is there a function that applies list of functions to a value?
AdamKal
adamkalinski at gmail.com
Wed Aug 28 09:23:47 EDT 2013
Thanks!
I guess this is as simple as it gets then. I was just looking for the "one obvious way to do it".
W dniu środa, 28 sierpnia 2013 15:11:34 UTC+2 użytkownik Tim Chase napisał:
> On 2013-08-28 05:52, AdamKal wrote:
>
> > From time to time I have to apply a series of functions to a value
>
> > in such a way:
>
> >
>
> > func4(func3(func2(func1(myval))))
>
> >
>
> > I was wondering if there is a function in standard library that
>
> > would take a list of functions and a initial value and do the above
>
> > like this:
>
> >
>
> > func_im_looking_for([func1, func2, func3, func4], myval)
>
>
>
> At least in Py2.x you can use reduce:
>
>
>
> reduce(lambda value, fn: fn(value), [list_of_functions], myval)
>
>
>
> I believe it was moved into functools.reduce() in Py3.x meaning you
>
> might want to do something like
>
>
>
> try:
>
> reduce # see if it's already in __builtins__
>
> except NameError: # running Py3...
>
> from functools import reduce
>
>
>
> or
>
>
>
> try:
>
> from functools import reduce
>
> except ImportError:
>
> pass # it should already be in __builtins__ for pre-2.6
>
>
>
> at the top to make sure it's in your global namespace.
>
>
>
> -tkc
More information about the Python-list
mailing list