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


Groups > comp.lang.python > #30838 > unrolled thread

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

Started byvasudevram <vasudevram@gmail.com>
First post2012-10-05 13:19 -0700
Last post2012-10-06 11:57 -0700
Articles 10 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  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

#30838 — fmap(), "inverse" of Python map() function

Fromvasudevram <vasudevram@gmail.com>
Date2012-10-05 13:19 -0700
Subjectfmap(), "inverse" of Python map() function
Message-ID<727ec5d8-d292-4012-ba87-da77a1040c6a@googlegroups.com>
http://jugad2.blogspot.in/2012/10/fmap-inverse-of-python-map-function.html

- Vasudev Ram
www.dancingbison.com
jugad2.blogspot.com
twitter.com/vasudevram

[toc] | [next] | [standalone]


#30841

FromIan Kelly <ian.g.kelly@gmail.com>
Date2012-10-05 15:31 -0600
Message-ID<mailman.1873.1349472728.27098.python-list@python.org>
In reply to#30838
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)

[toc] | [prev] | [next] | [standalone]


#30844

FromIan Kelly <ian.g.kelly@gmail.com>
Date2012-10-05 15:43 -0600
Message-ID<mailman.1875.1349473443.27098.python-list@python.org>
In reply to#30838
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

[toc] | [prev] | [next] | [standalone]


#30846

FromDevin Jeanpierre <jeanpierreda@gmail.com>
Date2012-10-05 18:52 -0400
Message-ID<mailman.1877.1349477604.27098.python-list@python.org>
In reply to#30838
On Fri, Oct 5, 2012 at 5: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.

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

[toc] | [prev] | [next] | [standalone]


#30849

FromIan Kelly <ian.g.kelly@gmail.com>
Date2012-10-05 17:24 -0600
Message-ID<mailman.1879.1349479486.27098.python-list@python.org>
In reply to#30838
On Fri, Oct 5, 2012 at 4:52 PM, Devin Jeanpierre <jeanpierreda@gmail.com> wrote:
> On Fri, Oct 5, 2012 at 5: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.
>
> 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.

[toc] | [prev] | [next] | [standalone]


#30852

FromDevin Jeanpierre <jeanpierreda@gmail.com>
Date2012-10-05 19:30 -0400
Message-ID<mailman.1882.1349479899.27098.python-list@python.org>
In reply to#30838
On Fri, Oct 5, 2012 at 7:24 PM, Ian Kelly <ian.g.kelly@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#30896

Fromvasudevram <vasudevram@gmail.com>
Date2012-10-06 11:57 -0700
Message-ID<a27b49d4-408a-4e98-8f63-6c2c9bb67538@googlegroups.com>
In reply to#30852
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

[toc] | [prev] | [next] | [standalone]


#30900

Fromvasudevram <vasudevram@gmail.com>
Date2012-10-06 15:07 -0700
Message-ID<9b8beb5d-001b-4606-9ca3-d57aed9862ba@googlegroups.com>
In reply to#30896
 
> 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

[toc] | [prev] | [next] | [standalone]


#30901

Fromvasudevram <vasudevram@gmail.com>
Date2012-10-06 15:07 -0700
Message-ID<mailman.1911.1349561230.27098.python-list@python.org>
In reply to#30896
 
> 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

[toc] | [prev] | [next] | [standalone]


#30897

Fromvasudevram <vasudevram@gmail.com>
Date2012-10-06 11:57 -0700
Message-ID<mailman.1908.1349549887.27098.python-list@python.org>
In reply to#30852
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web