Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Comment on

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

Javascript:

if (!Array.prototype.push) { /* IE < 5.5 lacks Array.push() */ Array.prototype.push=function(arg) { /* this is only the minimum required for map, push should +accept more than one parameter */ this[this.length]=arg; return this.length; } } /*** Bootstrap Perl ;-) ***/ Array.prototype.foreach=function(callback) { /* * callback is called with a single argument, the current a +rray element * callback return value is ignored * returns nothing */ for (var i=0; i<this.length; i++) { callback(this[i]); } }; Array.prototype.map=function(callback) { /* * callback is called with a single argument, the current a +rray element * callback returns an array of zero or more values * returns a new array of all callback return values */ var rv=new Array(); this.foreach(function(x) { callback(x).foreach(function(y) { rv.push(y); }); }); return rv; }; /*** List::Utils ***/ Array.prototype.reduce=function(callback) { /* * callback is called with two arguments, its last return v +alue (initially the first array element) and the next array e +lement * callback returns a single value used for the next iterat +ion * callback is not called when array contains less than two + elements */ if (this.length==0) return null; var rv=this[0]; for (var i=1; i<this.length; i++) { rv=callback(rv,this[i]); } return rv; }; /*** The real code ***/ roman_to_dec=(function(){ var rtoa={ M:1000, D:500, C:100, L:50, X:10, V:5, I:1 }; return function(str) { return str.toUpperCase().split('').map(function(_){ return [ rtoa[_] ]; }).reduce(function(a,b) { return a+b-a%b*2; }); }; })(); function main() { var testdata=["XLII", "LXIX", "mi"]; alert( testdata.map(function(_){ return [ _ + ": " + roman_to_dec(_) + "\n" ]; }).join("") ); };

Some notes:

  • There are no lists in Javascript, you have to use Arrays.
  • The Array.prototype.xxx=function(...) { ... }; notation adds new methods to ALL arrays, even after they were created. This is compareable to package Array; sub xxx { ... } -- except that Perl arrays aren't objects.
  • this is the current object. In the new Array methods, it is the array itself.
  • Javascript has anonymous functions, and you can / have to use them surprisingly often, because Javascript doesn't have code blocks.
  • You don't have anything like local in Javascript, so the implicit $_ used in Perl's map and foreach and $a and $b in reduce have to be passed explicitly to the callback.
  • The variable %rtoa is only available to roman_to_dec in the original perl code, and the same applies to my Javascript code. But because there are no code blocks, you need an anonymous function to create a private scope for rtoa. And that function needs to return the function that implements roman_to_dec, because otherwise there would be no way to access the roman_to_dec function. Finally, the anonymous function is called once with no arguments(), returning the implementation.

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: 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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 avoiding work at the Monastery: (21)
    As of 2015-07-02 17:59 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (44 votes), past polls