in reply to Re: "advanced" Perl functions and maintainability
in thread "advanced" Perl functions and maintainability
Um, why? What's wrong with foreach loops?Nothing, but the way you state this suggests there's something wrong with map and/or grep. Why is that?
IMO, foreach and map/grep are equivalent. Neither is inherently more clear than the other - people finding one construct more clear than the other usually base this preference (knowingly or not) on their experience with other languages. Many Perl programmers have learned programming in a language that has for/foreach like loops, but don't have map like constructs (C and Java for instance) - and many, if not all, Perl books and tutorials teach for/foreach long before they introduce map, and spend a lot more time explaining for/foreach than map or grep. People who are familiar with Lisp, Scheme, or other functional programming languages consider map/grep less exotic.
grep/map are great as list filters, when you want to build one list from another, but they force you to stuff all of your code in one tiny block/expression. foreach is the general-purpose list iterator, and you should always be ready to fallback on it if grep or map get too hairyEither you don't understand map/grep, you don't understand for/foreach, or you don't understand either. A foreach construct looks like:
while a map construct looks like:foreach (LIST) {BLOCK} # or foreach ITERATOR (LIST) {BLOCK}
So, either construct requires a keyword, a block and a list. The difference is that with one construct you place the list before the block (and need some addition keystrokes), and the block last. The other construct places the list at the end, with the block proceeding the list. In many cases, the blocks used can be identical - the block used by map can be as large as the one used by foreach (or should I say that foreach forces you to stuff all your code in a tiny block?).map {BLOCK} LIST; # or map EXPRESSION, LIST;
What was so objectionable about suggesting Perl programmers use a clearer construct over a shorter one?Nothing at all. What is objectionable is to suggest that you can objectively say that foreach is clearer than map. Stating that foreach is clearer than map just shows your inexperience in programming techniques.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: "advanced" Perl functions and maintainability
by William G. Davis (Friar) on Dec 13, 2004 at 13:43 UTC | |
by Anonymous Monk on Dec 13, 2004 at 14:34 UTC | |
by William G. Davis (Friar) on Dec 13, 2004 at 14:58 UTC | |
by Aristotle (Chancellor) on Dec 20, 2004 at 04:36 UTC |