Prev: LibXML (XPATH) and escape
Next: Module name access
From: John Bokma on 26 Jan 2010 18:08 Tad McClellan <tadmc(a)seesig.invalid> writes: > Arguments against obfuscation are NOT a matter of taste, they are > a matter of Good Programming Practice. Like I already wrote several times: I wouldn't use map in void context at this stage. But I don't think one /must/ (or /should/) not use it in void context. > Uri's argument was that map in void context conveys the wrong meaning. One could - to some extent - argue the same for using a 'for' for its aliasing effect in my opinion, e.g. my $string = 'hello, world'; for ( $string ) { s/e/a/; s/l/1/; } I consider the above a Perl idiom; one that I have no problem using. Yet I am sure it would confuse quite some beginning Perl programmers. To them it might convey a wrong meaning. But I don't like to dumb down my Perl code to a beginner's level. YMMV, in my opinion, it's opinion. And to avoid of being accused of "promoting map in void context", let me repeat again: I don't use map in void context, and when TEing a technical book I recommended the author not to use map in void context. But it's (no longer) a religious issue to me. -- John Bokma j3b Hacking & Hiking in Mexico - http://johnbokma.com/ http://castleamber.com/ - Perl & Python Development
From: Uri Guttman on 26 Jan 2010 18:11 >>>>> "BM" == Ben Morrow <ben(a)morrow.me.uk> writes: >> map conveys "I am building a list" (because it returns a list). BM> So does delete. Would you object to delete in void context? but delete conveys the message of doing a side effect. its return value is nice sugar but isn't needed (amazing how often i show use of delete's return value and how few know it). map's history (yes, history isn't taught much) is from building a list from a list. its primary purpose is building that list and that is what is says to the reader. i always teach code is for people, not computers. we all can use things in ways that were unintended but we try to stay away from them for fear of confusing readers. given that map has the for modifier for use when doing side effects, that is a better semantic match for that case. note that for modifier was added after perl5 was first out. map could have been used for that but given the inefficiency then of throwing out the list (not true anymore but still) and wrong semantics, it was a good addition to the language. so why not use it for its purpose? for means you are doing some side effects in the expression. map means you are building a list and supposedly not doing side effects. this is similar to using ?: with side effects. it is a bad idea even if you get the syntax right (using parens with assignments). ?: is meant to return a value and not for side effects. perl allows any legit expression including side effect ones anywhere. that doesn't mean it is a good idea to do that. code is for people, not computers. code is for OTHER people, not yourself. code is the record of the logical decisions you made when creating this program. code is where you can let your ego shine in the reflection of those who read your code. uri -- Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com -- ----- Perl Code Review , Architecture, Development, Training, Support ------ --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
From: Dr.Ruud on 26 Jan 2010 18:41 Tad McClellan wrote: > map conveys "I am building a list" (because it returns a list). IIRC: unless in void context. (as one of the recent optimizations) Not that I like to see map used in void context, I rather see a for loop. Just joking: perl -wle ' my @x = -3 .. 3; grep { ++$_; 0 } @x; print for @x; ' -2 -1 0 1 2 3 4 (huh? C<grep ++$_, -3..3> doesn't complain about readonly-ness) -- Ruud
From: sreservoir on 26 Jan 2010 19:07 On 1/26/2010 6:41 PM, Dr.Ruud wrote: > Tad McClellan wrote: > >> map conveys "I am building a list" (because it returns a list). > > IIRC: unless in void context. > (as one of the recent optimizations) not that that really matters. > Not that I like to see map used in void context, > I rather see a for loop. > > > Just joking: > > perl -wle ' > my @x = -3 .. 3; > grep { ++$_; 0 } @x; > print for @x; > ' > -2 > -1 > 0 > 1 > 2 > 3 > 4 > perl -wle 'my @a = -3 .. 3; map { --$_; () } @a; print for @a' not much worse, but still hell for readability. > (huh? C<grep ++$_, -3..3> doesn't complain about readonly-ness) (huh? it's rw pass-by-ref) -- "Six by nine. Forty two." "That's it. That's all there is." "I always thought something was fundamentally wrong with the universe"
From: Ben Morrow on 26 Jan 2010 20:42
Quoth sreservoir <sreservoir(a)gmail.com>: > On 1/26/2010 6:41 PM, Dr.Ruud wrote: > > > (huh? C<grep ++$_, -3..3> doesn't complain about readonly-ness) > > (huh? it's rw pass-by-ref) $_ ought to be an alias to a constant. ~% perl -e'map $_++, -3..3' ~% perl -e'map $_++, -3, -2, -1, 0, 1, 2, 3' Modification of a read-only value attempted at -e line 1. ~% In fact, it seems perl precomputes the range at compile-time, puts the results in a constant array, but forgets to make the elements constant: ~% perl -e'map $_++, -3..3' a <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v:{ ->3 7 <|> mapwhile(other->8)[t5] vK ->a 6 <@> mapstart K ->7 3 <0> pushmark s ->4 - <1> null lK/1 ->4 9 <1> postinc[t1] sK/1 ->7 - <1> ex-rv2sv sKRM/1 ->9 8 <$> gvsv(*_) s ->9 5 <1> rv2av lKPM/1 ->6 4 <$> const(AV ) s ->5 which has some side-effects which make this definitely a bug: ~% perl -E'for (1..2) { map {say ++$_} -2..2 }' -1 0 1 2 3 0 1 2 3 4 ~% Ben |