Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Rosetta PGA-TRAM

by afoken (Parson)
on Jun 23, 2009 at 21:02 UTC ( #774175=note: print w/ replies, xml ) Need Help??


in reply to Re: Rosetta PGA-TRAM
in thread Rosetta PGA-TRAM

Hmmm, foreach, map, and reduce seem to be ideas that are too good to be left out from Javascript, as I found out lately. https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array has all the details. Unfortunately, those features require JS 1.6 or even 1.8, despite naming 1.5 in the URL. The 1.5 in the URL is not completely wrong, as the site gives copy-and-paste-able replacement implementations in Javascript 1.5, so you can also use the methods in older browsers.

foreach was camelCased to forEach, Perl's grep is named filter. There is also a reduceRight method that works from right to left.

The calling conventions pass the current element, the index into the array, and the array object itself to the callback. While one could construct examples where the extra parameters could be useful, I think this "tastes" bad: No callback should need to access (and modify) the original array while iterating over the array. No callback should need to know the current index into the array. If it does, you are probably reinventing push, pop, shift, unshift, or splice.

The new methods allow specifying a different object for this inside the callback, which can be useful sometimes. If you don't specify an object, the array object is used.

Someone also recognised that map could be useful outside array contexts and constructed this piece of code. It just hurts my eyes.

var a = Array.prototype.map.call("Hello World", function(x) { return x +.charCodeAt(0); }) // a now equals [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]

(Source: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/map)

This code works "by accident", because Array.prototype.map can iterate over everything that behaves like an array, not only over real Arrays. The call method available on all Function objects allows to call a method of one object with this set to another object (the first argument passed to call). In this case, this is set to an instance of a String object, over which the Array.prototype.map method iterates.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)


Comment on Re^2: Rosetta PGA-TRAM
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (13)
As of 2014-07-30 09:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (230 votes), past polls