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". ;-)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
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, details, 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, summary, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.