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


Groups > comp.lang.python > #30844

Re: fmap(), "inverse" of Python map() function

References <727ec5d8-d292-4012-ba87-da77a1040c6a@googlegroups.com> <CALwzidmE5_LyKfEGqAGeN0Uv+g1fFo+ztjhZdr3b3vbRRnTvJA@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2012-10-05 15:43 -0600
Subject Re: fmap(), "inverse" of Python map() function
Newsgroups comp.lang.python
Message-ID <mailman.1875.1349473443.27098.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, Oct 5, 2012 at 3:31 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> On Fri, Oct 5, 2012 at 2:19 PM, vasudevram <vasudevram@gmail.com> wrote:
>>
>> http://jugad2.blogspot.in/2012/10/fmap-inverse-of-python-map-function.html
>
> Your fmap is a special case of reduce.
>
> def fmap(functions, argument):
>     return reduce(lambda result, func: func(result), functions, argument)

In a more functional style, you could also use reduce to compose the
functions before applying them:

def compose(f, g):
    return lambda x: f(g(x))

def fmap(functions):
    return reduce(compose, reversed(functions))

# Allowing you to then do:
result = fmap(functions)(argument)

Cheers,
Ian

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

fmap(), "inverse" of Python map() function vasudevram <vasudevram@gmail.com> - 2012-10-05 13:19 -0700
  Re: fmap(), "inverse" of Python map() function Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-05 15:31 -0600
  Re: fmap(), "inverse" of Python map() function Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-05 15:43 -0600
  Re: fmap(), "inverse" of Python map() function Devin Jeanpierre <jeanpierreda@gmail.com> - 2012-10-05 18:52 -0400
  Re: fmap(), "inverse" of Python map() function Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-05 17:24 -0600
  Re: fmap(), "inverse" of Python map() function Devin Jeanpierre <jeanpierreda@gmail.com> - 2012-10-05 19:30 -0400
    Re: fmap(), "inverse" of Python map() function vasudevram <vasudevram@gmail.com> - 2012-10-06 11:57 -0700
      Re: fmap(), "inverse" of Python map() function vasudevram <vasudevram@gmail.com> - 2012-10-06 15:07 -0700
      Re: fmap(), "inverse" of Python map() function vasudevram <vasudevram@gmail.com> - 2012-10-06 15:07 -0700
    Re: fmap(), "inverse" of Python map() function vasudevram <vasudevram@gmail.com> - 2012-10-06 11:57 -0700

csiph-web