Click here to get back home

The map function

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
The map function mynews 04-18-2008
| |--> Re: The map function Gunnar Hjalmars...04-23-2008
Posted by mynews on April 18, 2008, 11:30 pm
Please log in for more thread options
Whis is difference between map and for-each?
Why the map function is fast than for-each? <== is it for all case?



Posted by Ben Bullock on April 18, 2008, 11:42 pm
Please log in for more thread options
On Sat, 19 Apr 2008 11:30:05 +0800, mynews wrote:

> Whis is difference between map and for-each?

perldoc -f map

> Why the map function is
> fast than for-each? <== is it for all case?

Could you post an example piece of code where the map function is faster
than foreach?

Posted by Jürgen Exner on April 18, 2008, 11:49 pm
Please log in for more thread options
>Whis is difference between map and for-each?

They have very little in comon except that both loop over the elements
of a list. map() is a function, foreach is a statement modifier or a
compound (loop) statement.

While sometimes map() can be used to achive similar results as a
foreach, usually that's not a good idea because you create a return
value only to throw it away.
Vice-versa you can use map() to return a completely different list than
its argument while modifying the list of a foreach loop is strongly
discouraged and may lead to very unexpected results.
Also closures are more natural with map().

>Why the map function is fast than for-each? <== is it for all case?

It is? That would surprise me, but I haven't run any benchmarks.

jue

Posted by Gerry Ford on April 19, 2008, 5:40 pm
Please log in for more thread options

>>Whis is difference between map and for-each?
>
> They have very little in comon except that both loop over the elements
> of a list. map() is a function, foreach is a statement modifier or a
> compound (loop) statement.
>
> While sometimes map() can be used to achive similar results as a
> foreach, usually that's not a good idea because you create a return
> value only to throw it away.
> Vice-versa you can use map() to return a completely different list than
> its argument while modifying the list of a foreach loop is strongly
> discouraged and may lead to very unexpected results.
> Also closures are more natural with map().
>
>>Why the map function is fast than for-each? <== is it for all case?
>
> It is? That would surprise me, but I haven't run any benchmarks.

I think of foreach as a loop and map as a function like this:
map ($_->[$subject_offset].' from '.$_->[$from_offset], @xover)

They don't seem like comparables to me. Of course, there could be a lot
more happening in the above line than I realize. I haven't encountered
foreach as a statement modifier yet.
--
"A belief in a supernatural source of evil is not necessary; men alone
are quite capable of every wickedness."

~~ Joseph Conrad (1857-1924), novelist



Posted by Tad J McClellan on April 20, 2008, 9:33 am
Please log in for more thread options
>
>>>Whis is difference between map and for-each?
>>
>> They have very little in comon except that both loop over the elements
>> of a list. map() is a function, foreach is a statement modifier or a
>> compound (loop) statement.
>>
>> While sometimes map() can be used to achive similar results as a
>> foreach, usually that's not a good idea because you create a return
>> value only to throw it away.
>> Vice-versa you can use map() to return a completely different list than
>> its argument while modifying the list of a foreach loop is strongly
>> discouraged and may lead to very unexpected results.
>> Also closures are more natural with map().
>>
>>>Why the map function is fast than for-each? <== is it for all case?
>>
>> It is? That would surprise me, but I haven't run any benchmarks.
>
> I think of foreach as a loop and map as a function like this:
> map ($_->[$subject_offset].' from '.$_->[$from_offset], @xover)


You didn't show where the return value from map() is going, I'll assume:

@list = map ($_->[$subject_offset]...


> They don't seem like comparables to me.


@list = ();
foreach ( @xover ) { # untested
push @list, $_->[$subject_offset].' from '.$_->[$from_offset];
}


The contents of @list are the same either way.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher0cmdat/"

Similar ThreadsPosted
Find function definition in a C file for a function with its name and starting line no. as input March 24, 2006, 12:08 am
Strange Perl line : Return the result of a function to a function October 24, 2006, 9:19 am
Can I use a function ref to call a function in a web script? October 2, 2008, 2:50 pm
XQL 'id' function in XML::XQL November 15, 2005, 8:14 pm
The vec() function December 21, 2006, 1:25 am
using a function with net::ssh March 9, 2007, 5:35 pm
using a function in map? November 15, 2007, 6:20 am
Using END and the die function February 8, 2008, 2:07 pm
wordwrap function July 14, 2004, 11:11 pm
QC: good function? July 16, 2004, 12:24 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap