Can Your Programming Language Do This?
August 5th, 2006
Yes, Ruby can do this
But seeing as I am a fan of Object Oriented programming. (Ruby in particular)
I would change:
function map(fn, a)
{
for (i = 0; i < a.length; i++)
{
a[i] = fn(a[i]);
}
}
to:
Array.prototype.map = function( fn ){
result = new Array();
for ( var i = 0; i < this.length; i++ ){
result[i] = fn(this[i]);
}
return result;
}
now you can do:
var a = [1,2,3];
a.map( function(num){return num*2;} );
a.map( alert );
Leave a Reply