Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^4: Replacing whitespaces with a regex

by topher (Scribe)
on Mar 05, 2013 at 01:58 UTC ( #1021735=note: print w/ replies, xml ) Need Help??


in reply to Re^3: Replacing whitespaces with a regex
in thread Replacing whitespaces with a regex

I'm a huge fan of map, but I wouldn't use it that way. Most recommendations on Perl best practices that I've seen suggest against map in void context (using for/foreach instead), and I think that one makes a lot of sense.

When I see map, I expect that it is being used in the conventional higher-order function way: taking a list, applying a given function to each item in the list, and returning the resulting list. The original array isn't changed.

This gives me a strong visual cue when I'm reading code: if I see map, it's creating a (modified) copy of a list, if I see for/foreach loop, then I'm making a change to the list itself. I would go so far as to say that these are the fairly standard (in so far as Perl has standards) and idiomatic way of using map and for/foreach.

Just as a historical side-note, map isn't a Perl creation; it comes from the world of functional programming. This is also where the recommended usage (as suggested above) comes from, as that is how it is used in most languages. Many languages have a map function (including Python).

You are, of course, free to use it any way you want, but don't be surprised if most programmers (especially those familiar with functional programming) expect it to be used as described above.

Christopher Cashell


Comment on Re^4: Replacing whitespaces with a regex
Select or Download Code
Re^5: Replacing whitespaces with a regex
by ISAI student (Scribe) on Mar 20, 2013 at 18:47 UTC

    I have learned something new. Thanks.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1021735]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2014-07-22 23:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (130 votes), past polls