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

Discussion in 'Python' started by vasudevram, Oct 5, 2012.

  1. vasudevram

    vasudevram Guest

    vasudevram, Oct 5, 2012
    #1
    1. Advertising

  2. vasudevram

    Ian Kelly Guest

    Ian Kelly, Oct 5, 2012
    #2
    1. Advertising

  3. vasudevram

    Ian Kelly Guest

    On Fri, Oct 5, 2012 at 3:31 PM, Ian Kelly <> wrote:
    > On Fri, Oct 5, 2012 at 2:19 PM, vasudevram <> 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
     
    Ian Kelly, Oct 5, 2012
    #3
  4. On Fri, Oct 5, 2012 at 5:31 PM, Ian Kelly <> wrote:
    > On Fri, Oct 5, 2012 at 2:19 PM, vasudevram <> wrote:
    >>
    >> http://jugad2.blogspot.in/2012/10/fmap-inverse-of-python-map-function.html

    >
    > Your fmap is a special case of reduce.


    So is map.

    def map(f, seq):
    return reduce(
    lambda rseq, newpre:
    rseq.append(f(newpre)) or rseq,
    seq,
    [])

    "X is a special case of reduce" is basically the same as saying "X can
    be implemented using a for loop". If it's meant as a complaint, it's a
    poor one.

    -- Devin
     
    Devin Jeanpierre, Oct 5, 2012
    #4
  5. vasudevram

    Ian Kelly Guest

    On Fri, Oct 5, 2012 at 4:52 PM, Devin Jeanpierre <> wrote:
    > On Fri, Oct 5, 2012 at 5:31 PM, Ian Kelly <> wrote:
    >> On Fri, Oct 5, 2012 at 2:19 PM, vasudevram <> wrote:
    >>>
    >>> http://jugad2.blogspot.in/2012/10/fmap-inverse-of-python-map-function.html

    >>
    >> Your fmap is a special case of reduce.

    >
    > So is map.


    I realize that. My point is that the function *feels* more like a
    variant of reduce than of map.

    > If it's meant as a complaint, it's a poor one.


    It's not.
     
    Ian Kelly, Oct 6, 2012
    #5
  6. On Fri, Oct 5, 2012 at 7:24 PM, Ian Kelly <> wrote:
    > I realize that. My point is that the function *feels* more like a
    > variant of reduce than of map.
    >
    >> If it's meant as a complaint, it's a poor one.

    >
    > It's not.


    Fair enough all around. Sorry for misunderstanding.

    -- Devin
     
    Devin Jeanpierre, Oct 6, 2012
    #6
  7. vasudevram

    vasudevram Guest

    On Saturday, October 6, 2012 5:01:40 AM UTC+5:30, Devin Jeanpierre wrote:
    > On Fri, Oct 5, 2012 at 7:24 PM, Ian Kelly wrote:
    >
    > > I realize that. My point is that the function *feels* more like a

    >
    > > variant of reduce than of map.

    >
    > >

    >
    > >> If it's meant as a complaint, it's a poor one.

    >
    > >

    >
    > > It's not.

    >
    >
    >
    > Fair enough all around. Sorry for misunderstanding.
    >
    >
    >
    > -- Devin


    Thanks to all who replied. Always good to learn something new.

    - Vasudev
     
    vasudevram, Oct 6, 2012
    #7
  8. vasudevram

    vasudevram Guest

    On Saturday, October 6, 2012 5:01:40 AM UTC+5:30, Devin Jeanpierre wrote:
    > On Fri, Oct 5, 2012 at 7:24 PM, Ian Kelly wrote:
    >
    > > I realize that. My point is that the function *feels* more like a

    >
    > > variant of reduce than of map.

    >
    > >

    >
    > >> If it's meant as a complaint, it's a poor one.

    >
    > >

    >
    > > It's not.

    >
    >
    >
    > Fair enough all around. Sorry for misunderstanding.
    >
    >
    >
    > -- Devin


    Thanks to all who replied. Always good to learn something new.

    - Vasudev
     
    vasudevram, Oct 6, 2012
    #8
  9. vasudevram

    vasudevram Guest


    > Thanks to all who replied. Always good to learn something new.


    P.S. A reader posted a good comment with Scala as well as Python code for a compose function (basically same functionality as fmap, or more - the compose once, run many times thing). It's the 4th comment on my blog post.

    - Vasudev
     
    vasudevram, Oct 6, 2012
    #9
  10. vasudevram

    vasudevram Guest


    > Thanks to all who replied. Always good to learn something new.


    P.S. A reader posted a good comment with Scala as well as Python code for a compose function (basically same functionality as fmap, or more - the compose once, run many times thing). It's the 4th comment on my blog post.

    - Vasudev
     
    vasudevram, Oct 6, 2012
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    869
    David Bishop
    Jul 17, 2005
  2. Larry Coon

    Inverse get() for a Map

    Larry Coon, Jul 7, 2004, in forum: Java
    Replies:
    8
    Views:
    713
    Larry Coon
    Jul 11, 2004
  3. =?iso-8859-1?B?bW9vcJk=?=

    Asking inverse() function for List and ArrayList

    =?iso-8859-1?B?bW9vcJk=?=, Dec 12, 2005, in forum: Java
    Replies:
    11
    Views:
    682
    andreaz
    Dec 13, 2005
  4. David C. Fox

    inverse of the zip function

    David C. Fox, Jul 29, 2003, in forum: Python
    Replies:
    6
    Views:
    398
    Raymond Hettinger
    Jul 30, 2003
  5. Pasacco

    inverse function, how?

    Pasacco, Nov 29, 2006, in forum: VHDL
    Replies:
    3
    Views:
    2,169
    Pasacco
    Nov 29, 2006
Loading...

Share This Page