in reply to Re: Think for yourself.
in thread is the use of map in a void context deprecated ?
What is the difference between what these lines should do?They should do the same thing. But to my eyes the second reads much more clearly, and I believe that the same holds true at virtually any level of Perl expertise.map do_something($_), @some_list; do_something($_) for @some_list;
Well, they don't do the same thing; or rather, you cannot deduce from this code fragment whether they will do the same thing or not. If you want to be sure they are the same, you have to write the latter line as:
Context matters. map gives list context to the expression, while the expression with the for-modifier is in void context.() = do_something ($_) for @some_list;
And of course, there's another common way of writing map expressions:
Writing that in for-modifier style gives you:map {BLOCK} @some_list;
Although if the context doesn't matter, you can get away with:() = do {BLOCK} for @some_list;
Alternatively, you can write it as for statements:do {BLOCK} for @some_list;
orfor (@some_list) { () = do {BLOCK}; }
depending on whether context matters or not.for (@some_list) { BLOCK; }
If there is a clear way and an unclear way of writing the same thing, with both taking similar effort and length, I call it bad style to deliberately use the unclear one (unless confusion is your goal).What is unclear and what is clear is very subjective. For a language that comes with opinions on what is good and what is bad style, see http://www.python.org. However, I find it difficult to believe there are people that find map in void context "unclear", "obfuscated" or "bad style". I can certainly understand people having problems with map. But I find it hard to believe there are seasoned Perl programmers that have no problem with map if the map is on the right hand side of an assignment, but are suddenly getting confused if they assignment disappears.
What is your feeling if you see:
Utter confusion? Or is this ok, as long as it's not called 'map'?user_defined_function {BLOCK} LIST;
This does not, of course, justify deriding someone who has picked up the meme of map in void context. But it does indicate gently pointing out that there are clearer ways to do the same thing.I disagree. It's ok to say that you have problems understanding the concept of map in void context, and that you prefer another style. But it's not ok to suggest that getting confused over a map in void context is something that all Perl programmers suffer from. Just point out it's your own personal preference.
Abigail
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Think for yourself.
by demerphq (Chancellor) on Oct 06, 2003 at 15:54 UTC | |
by Abigail-II (Bishop) on Oct 06, 2003 at 19:50 UTC | |
by demerphq (Chancellor) on Oct 06, 2003 at 23:30 UTC | |
by Abigail-II (Bishop) on Oct 07, 2003 at 09:29 UTC | |
by Aristotle (Chancellor) on Oct 07, 2003 at 22:59 UTC | |
by Petruchio (Vicar) on Oct 07, 2003 at 15:07 UTC | |
by Abigail-II (Bishop) on Oct 07, 2003 at 15:24 UTC | |
by Aristotle (Chancellor) on Oct 06, 2003 at 20:08 UTC | |
Re: Re: Think for yourself.
by tilly (Archbishop) on Oct 07, 2003 at 05:53 UTC | |
by Abigail-II (Bishop) on Oct 07, 2003 at 09:39 UTC | |
by tilly (Archbishop) on Oct 08, 2003 at 02:43 UTC | |
by zby (Vicar) on Oct 08, 2003 at 10:27 UTC |
In Section
Seekers of Perl Wisdom