Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > The map function

Reply
Thread Tools

The map function

 
 
mynews
Guest
Posts: n/a
 
      04-19-2008
Whis is difference between map and for-each?
Why the map function is fast than for-each? <== is it for all case?


 
Reply With Quote
 
 
 
 
Ben Bullock
Guest
Posts: n/a
 
      04-19-2008
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?
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      04-19-2008
"mynews" <(E-Mail Removed)> wrote:
>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
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      04-20-2008
Gerry Ford <(E-Mail Removed)> wrote:
>
> "Jürgen Exner" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> "mynews" <(E-Mail Removed)> wrote:
>>>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.noitatibaher\100cmdat/"
 
Reply With Quote
 
nolo contendere
Guest
Posts: n/a
 
      04-20-2008
On Apr 18, 11:49*pm, Jürgen Exner <(E-Mail Removed)> wrote:
> "mynews" <(E-Mail Removed)> wrote:
> >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.

Is this still true? I thought map in void context was optimized.
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      04-21-2008
>>>>> "GF" == Gerry Ford <(E-Mail Removed)> writes:

GF> Maybe we can use this script to compare and contrast:
GF> sub read_killrc {
GF> open(FILE, "<$killrc") and do {
GF> @filter = ();

not needed in the map version

GF> foreach (<FILE>) {
GF> chomp; length or next; /^#/o and next;
GF> my $pat; eval '$pat = qr/$_/' or do {prompt $@; next};
GF> push @filter, $pat;
GF> }
GF> close(FILE);

GF> What would this syntax look like with a map function?

untested:

return unless open(my $file, "<$killrc") ;
return map {
/^([^#]+)#?$/ &&
eval{ qr/$1/} || prompt $@, ()
} <$file> ;

maybe the return failure value should be grepped out but that is a minor
style change.

map makes tight loops like that much cleaner. they remove the collecting
array and its push and uses less perl which generally makes it faster
(in the right conditions, especially with shorter arrays)

map is such a basic concept i wonder why it seems to be a stumbling
block for so many newbies. year after year we get the same map
queries. what use is it? how do i convert loops to maps? is it faster or
slower? void map is ok? some are in the faq but there must be something
missing if this perl learning speed bump always seems to hit so many.

uri

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
Ben Bullock
Guest
Posts: n/a
 
      04-21-2008
On Sun, 20 Apr 2008 15:30:06 -0700, nolo contendere wrote:

> On Apr 18, 11:49Â*pm, Jürgen Exner <(E-Mail Removed)> wrote:


>> 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.


> Is this still true? I thought map in void context was optimized.


There was a discussion about this topic a month ago (started March 22).
Perhaps reading this might help:

http://groups.google.co.jp/group/com...ch?hl=en&q=map
+void+context&start=0&scoring=d&hl=en&
 
Reply With Quote
 
Charlton Wilbur
Guest
Posts: n/a
 
      04-21-2008
>>>>> "UG" == Uri Guttman <(E-Mail Removed)> writes:

UG> map is such a basic concept i wonder why it seems to be a
UG> stumbling block for so many newbies. year after year we get
UG> the same map queries.

Because most of the time the newbies have run into the other basic
concepts elsewhere -- for loops, if/then, subroutines, etc., even
object orientation -- but it's very likely that Perl is their first
exposure to map and functional programming.

Charlton


--
Charlton Wilbur
(E-Mail Removed)
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      04-21-2008
Uri Guttman <(E-Mail Removed)> wrote:
>map is such a basic concept i wonder why it seems to be a stumbling
>block for so many newbies.


Actually map() is not as trivial, because it is a higher order function,
i.e. a function that takes a function (or code block) as argument.
I've seen this again and again when we tought functional programming
that there are otherwise smart students, who simply couldn't grasp the
concept that a function can be data (i.e. argument to another function),
too.
And considering that not many students are tought functional programming
at all and that most well-known languages don't have higher-order
functions at all it is not surprising that map() is a speed bump for
many.
Another nice example is sort(). I wonder how many implemented their own
sorting algorithm because they couldn't figure out how to write the
comparision function for non-trivial sorts.

jue
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      04-21-2008
>>>>> "JE" == Jürgen Exner <(E-Mail Removed)> writes:

JE> Uri Guttman <(E-Mail Removed)> wrote:
>> map is such a basic concept i wonder why it seems to be a stumbling
>> block for so many newbies.


JE> Actually map() is not as trivial, because it is a higher order
JE> function, i.e. a function that takes a function (or code block) as
JE> argument. I've seen this again and again when we tought
JE> functional programming that there are otherwise smart students,
JE> who simply couldn't grasp the concept that a function can be data
JE> (i.e. argument to another function), too.

coming from assembler where i passed around pointers to code all the
time and similarly i did that in c and i knew list, yes, functional
programming is a core thing with me. but if you teach references first
including dispatch tables, then shifting to functional programming and
map should be easy. i do training too and i don't find it hard to teach
map/grep but they do need some handholding.

JE> Another nice example is sort(). I wonder how many implemented
JE> their own sorting algorithm because they couldn't figure out how
JE> to write the comparision function for non-trivial sorts.

that is one reason i wrote sort::maker!

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
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
std::map::find() throws exception when map is empty? Matthias Hildebrand C++ 5 03-20-2012 06:09 AM
I can map all files (.*) to asp.net worker.How do I map NO FILE to asp.net worker? alex ASP .Net 1 02-04-2005 03:18 AM
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
map.insert(key,val) vs. map[key]=val ? Patrick Guio C++ 6 10-20-2004 01:54 PM
map that maps to iterators in the same map ? Vlad C++ 0 12-15-2003 08:29 PM



Advertisments