Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Rosetta PGA-TRAM

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


in reply to Rosetta PGA-TRAM

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


Comment on Re: Rosetta PGA-TRAM
Select or Download Code
Re^2: Rosetta PGA-TRAM
by afoken (Parson) on Jun 23, 2009 at 21:02 UTC

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2014-12-25 17:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (161 votes), past polls