Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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". ;-)

In reply to Re^2: Rosetta PGA-TRAM by afoken
in thread Rosetta PGA-TRAM by eyepopslikeamosquito

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (12)
    As of 2014-10-01 20:32 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      What is your favourite meta-syntactic variable name?














      Results (38 votes), past polls