Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   fmap(), "inverse" of Python map() function (http://www.velocityreviews.com/forums/t953091-fmap-inverse-of-python-map-function.html)

vasudevram 10-05-2012 08:19 PM

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

http://jugad2.blogspot.in/2012/10/fm...-function.html

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

Ian Kelly 10-05-2012 09:31 PM

Re: fmap(), "inverse" of Python map() function
 
On Fri, Oct 5, 2012 at 2:19 PM, vasudevram <vasudevram@gmail.com> wrote:
>
> http://jugad2.blogspot.in/2012/10/fm...-function.html


Your fmap is a special case of reduce.

def fmap(functions, argument):
return reduce(lambda result, func: func(result), functions, argument)

Ian Kelly 10-05-2012 09:43 PM

Re: fmap(), "inverse" of Python map() function
 
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/fm...-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

Devin Jeanpierre 10-05-2012 10:52 PM

Re: fmap(), "inverse" of Python map() function
 
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/fm...-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

Ian Kelly 10-05-2012 11:24 PM

Re: fmap(), "inverse" of Python map() function
 
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/fm...-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.

Devin Jeanpierre 10-05-2012 11:30 PM

Re: fmap(), "inverse" of Python map() function
 
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

vasudevram 10-06-2012 06:57 PM

Re: fmap(), "inverse" of Python map() function
 
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 10-06-2012 06:57 PM

Re: fmap(), "inverse" of Python map() function
 
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 10-06-2012 10:07 PM

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

> 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 10-06-2012 10:07 PM

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

> 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


All times are GMT. The time now is 06:43 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57