foldr function in Python

Ant antroy at gmail.com
Fri Nov 23 05:29:07 EST 2007


On Nov 23, 9:31 am, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
> On Fri, 23 Nov 2007 00:50:30 -0800, Ant wrote:
> > So my point really is that foldr (perhaps renamed to make_reducer or
> > something) could create idioms that are more readable than using
> > reduce directly.
>
> The name is definitely not so good because there is a `foldr` in Haskell

I haven't done any Haskell programming for maybe 6 years, so can't
remember a thing about it :-) I assume then that in Haskell currying
is automatic - i.e. if you leave out arguments you get a curried
function back, is that correct?

>  foldr = lambda func, initial, iterable: reduce(func, iterable, initial)
>
>  comma_separate = partial(foldr, insert_comma, '')
>
> `insert_comma()` is left as an exercise for the reader.  :-)
>
> I think that's better than a `make_reducer()`.
>
> Of course it's a silly example because the "pythonic" way to define
> `comma_separate()` is::
>
>  comma_separate = ','.join

Well, yes of course. I have no use cases for this, hence the silly
example :-). It just occurred  to me that it may be a more readable
solution than using reduce. But make_reducer is I agree a crap name,
and I can't think of a better one offhand. And I think that this would
only be valuable if an intuitive name could be found, otherwise people
reading the code would have the extra overhead of working out what the
function actually does...





More information about the Python-list mailing list