|
Prev: FAQ 4.44 How do I test whether two arrays or hashes are equal?
Next: FAQ 5.3 How do I count the number of lines in a file?
From: Charlton Wilbur on 21 Apr 2008 11:13 >>>>> "UG" == Uri Guttman <uri(a)stemsystems.com> 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 cwilbur(a)chromatico.net
From: J�rgen Exner on 21 Apr 2008 12:56 Uri Guttman <uri(a)stemsystems.com> 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
From: Uri Guttman on 21 Apr 2008 13:46 >>>>> "JE" == J�rgen Exner <jurgenex(a)hotmail.com> writes: JE> Uri Guttman <uri(a)stemsystems.com> 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 ------ uri(a)stemsystems.com -------- 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 ---------
From: Gerry Ford on 21 Apr 2008 21:47 "Uri Guttman" <uri(a)stemsystems.com> wrote in message news:x7fxtfeojz.fsf(a)mail.sysarch.com... >>>>>> "GF" == Gerry Ford <gerry(a)nowhere.ford> 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. I tried both versions of this without any success whatsoever: #!/usr/bin/perl -w use strict; my $killrc = "sample.killrc"; my @filter; my @list1 = qw( Mon Tu Wed); open(FILE, "<$killrc") and do { @filter = (); foreach (<FILE>) { chomp; length or next; /^#/o and next; my $pat; eval '$pat = qr/$_/' or do {prompt $@; next}; push @filter, $pat; } close(FILE); } print @filter; print @list1; # perl mats5.pl 2>text50.txt >text51.txt __END__ #!/usr/bin/perl -w use strict; my $killrc = "sample.killrc"; return unless open(my $file, "<$killrc") ; return map { /^([^#]+)#?$/ && eval{ qr/$1/} || prompt $@, () } <$file> ; # perl mats4.pl 2>text50.txt >text51.txt __END__ Perl is so idiomatic, it makes me scream. The syntax for the print statement is: print LIST How this doesn't fit the bill is beyond me. print @list1; I find no example of printing a list in the camel book. It does have print reverse sort map (1c) keys %hash; -- "A belief in a supernatural source of evil is not necessary; men alone are quite capable of every wickedness." ~~ Joseph Conrad (1857-1924), novelist
From: Uri Guttman on 21 Apr 2008 23:50
>>>>> "GF" == Gerry Ford <gerry(a)nowhere.ford> writes: GF> "Uri Guttman" <uri(a)stemsystems.com> wrote in message >> untested: >> >> return unless open(my $file, "<$killrc") ; >> return map { >> /^([^#]+)#?$/ && >> eval{ qr/$1/} || prompt $@, () >> } <$file> ; >> GF> I tried both versions of this without any success whatsoever: how about showing your output? GF> #!/usr/bin/perl -w use warnings is better than -w GF> use strict; GF> my $killrc = "sample.killrc"; GF> my @filter; GF> my @list1 = qw( Mon Tu Wed); where did that come from? GF> open(FILE, "<$killrc") and do { GF> @filter = (); 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> } GF> print @filter; GF> print @list1; and the results? GF> # perl mats5.pl 2>text50.txt >text51.txt why the redirect of stderr? there is no stderr output. and what is in the prompt sub? GF> __END__ GF> #!/usr/bin/perl -w GF> use strict; GF> my $killrc = "sample.killrc"; my return calls IMPLY A SUB is surrounding it. of course it won't work as shown. jeez, i can't hold your hand and also blow your nose. think about the code and don't just copy/run it. GF> return unless open(my $file, "<$killrc") ; GF> return map { GF> /^([^#]+)#?$/ && GF> eval{ qr/$1/} || prompt $@, () GF> } <$file> ; GF> # perl mats4.pl 2>text50.txt >text51.txt GF> __END__ GF> Perl is so idiomatic, it makes me scream. The syntax for the print GF> statement is: GF> print LIST GF> How this doesn't fit the bill is beyond me. GF> print @list1; huh? show a complete runnable example of why you think that doesn't work. my guess is that @list1 has no newlines and so doesn't get printed until the program ends. this is a well know buffering issue and isn't a bug in perl. GF> I find no example of printing a list in the camel book. It does have GF> print reverse sort map (1c) keys %hash; and in the FAQ? on cpan? the camel book is just a book. it can't have every possible example you want. and also note that the perl docs are the actually final language description, not the camel books. uri -- Uri Guttman ------ uri(a)stemsystems.com -------- 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 --------- |