in reply to Re: map in void context
in thread map in void context
Side-effects are something to be avoided when programming.In Perl? So, you'd prefer to avoid assignment, print statements, regular expressions, and even fetching the value of a variable?
There are programming languages that are more suitable for avoiding side-effects. But programming in Perl while avoiding side-effects makes as much sense as writing English novels while avoiding vowels.
I also like my code to be as self-documenting as possible.Who doesn't? For me having code as much self-documenting as possible doesn't mean map in void context isn't.
I find map is less than optimal for both of these reasons.The map-in-void context equivalent would be$sum += $index{$_} for split(//, $word);
I see as many side-effects as the code using 'for', so your example doesn't prove your first reason. In fact, the 'side-effect' reason people often come up with doesn't make any sense. If you do map EXPR, LIST or map BLOCK LIST, the side-effects are in EXPR or BLOCK. The 'for' equivalents are EXPR for LIST and for (LIST) BLOCK. Same EXPR and BLOCK. Same side-effects.map $sum += $index{$_}, split(//, $word);
As for documentation (or clearness) issues, I see differences between:
andmap {BLOCK} LIST
and that's the order of BLOCK and LIST. If I find the action more important, I prefer map {BLOCK} LIST, as it lists the statement(s) first. If I want to put the focus on the data, I use for (LIST) {BLOCK}. (There's also a difference in context, map gives list context, for void context, but that's seldomly an issue).for (LIST) {BLOCK}
I hardly ever use the map EXPR, LIST construct though (regardless of context), as it doesn't clearly separate the action from the data.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: map in void context
by kyle (Abbot) on Dec 17, 2008 at 16:45 UTC | |
by pat_mc (Pilgrim) on Dec 20, 2008 at 20:44 UTC | |
by chromatic (Archbishop) on Dec 20, 2008 at 21:09 UTC | |
by pat_mc (Pilgrim) on Dec 21, 2008 at 21:18 UTC | |
by tye (Sage) on Dec 21, 2008 at 22:59 UTC | |
by ikegami (Patriarch) on Dec 21, 2008 at 21:26 UTC | |
| |
by kyle (Abbot) on Dec 20, 2008 at 21:03 UTC | |
by ysth (Canon) on Dec 22, 2008 at 23:03 UTC | |
by pat_mc (Pilgrim) on Dec 21, 2008 at 21:02 UTC | |
by ikegami (Patriarch) on Dec 21, 2008 at 21:10 UTC | |
| |
by almut (Canon) on Dec 17, 2008 at 16:54 UTC | |
by JavaFan (Canon) on Dec 17, 2008 at 17:04 UTC | |
by JadeNB (Chaplain) on Dec 18, 2008 at 01:47 UTC | |
by JavaFan (Canon) on Dec 18, 2008 at 10:14 UTC | |
|
Re^3: map in void context
by ikegami (Patriarch) on Dec 17, 2008 at 17:24 UTC | |
|