Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > fmap(), "inverse" of Python map() function

Reply
Thread Tools

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

 
 
vasudevram
Guest
Posts: n/a
 
      10-05-2012

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

- Vasudev Ram
www.dancingbison.com
jugad2.blogspot.com
twitter.com/vasudevram
 
Reply With Quote
 
 
 
 
Ian Kelly
Guest
Posts: n/a
 
      10-05-2012
On Fri, Oct 5, 2012 at 2:19 PM, vasudevram <(E-Mail Removed)> 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)
 
Reply With Quote
 
 
 
 
Ian Kelly
Guest
Posts: n/a
 
      10-05-2012
On Fri, Oct 5, 2012 at 3:31 PM, Ian Kelly <(E-Mail Removed)> wrote:
> On Fri, Oct 5, 2012 at 2:19 PM, vasudevram <(E-Mail Removed)> 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
 
Reply With Quote
 
Devin Jeanpierre
Guest
Posts: n/a
 
      10-05-2012
On Fri, Oct 5, 2012 at 5:31 PM, Ian Kelly <(E-Mail Removed)> wrote:
> On Fri, Oct 5, 2012 at 2:19 PM, vasudevram <(E-Mail Removed)> 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
 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      10-05-2012
On Fri, Oct 5, 2012 at 4:52 PM, Devin Jeanpierre <(E-Mail Removed)> wrote:
> On Fri, Oct 5, 2012 at 5:31 PM, Ian Kelly <(E-Mail Removed)> wrote:
>> On Fri, Oct 5, 2012 at 2:19 PM, vasudevram <(E-Mail Removed)> 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.
 
Reply With Quote
 
Devin Jeanpierre
Guest
Posts: n/a
 
      10-05-2012
On Fri, Oct 5, 2012 at 7:24 PM, Ian Kelly <(E-Mail Removed)> 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
 
Reply With Quote
 
vasudevram
Guest
Posts: n/a
 
      10-06-2012
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
 
Reply With Quote
 
vasudevram
Guest
Posts: n/a
 
      10-06-2012
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
 
Reply With Quote
 
vasudevram
Guest
Posts: n/a
 
      10-06-2012

> 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
 
Reply With Quote
 
vasudevram
Guest
Posts: n/a
 
      10-06-2012

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function versus pointer to function, in context of std::function,huh? Alf P. Steinbach C++ 10 07-27-2011 05:51 AM
Function pointer to void function and int function Giannis Papadopoulos C Programming 5 09-05-2005 09:06 PM
How override ALL function calls? (Is there a "function call function"?) seberino@spawar.navy.mil Python 2 08-01-2005 12:38 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Passing a C++ object's member function to a C function expecing a function pointer! James Vanns C++ 7 01-21-2004 02:39 AM



Advertisments