Created
October 7, 2008 13:30
-
-
Save atifaziz/15290 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.range = function(start, end) | |
{ | |
if (!end) | |
{ | |
end = start; | |
start = 0; | |
} | |
var a = []; | |
for (var i = start; i < end; i++) | |
a.push(i); | |
return a; | |
} | |
Array.prototype.each = function(callback) | |
{ | |
for (var i = 0; i < this.length; i++) | |
callback(this[i], i); | |
} | |
Array.prototype.filter = function(condition) | |
{ | |
var a = []; | |
this.each(function(v) { if (condition(v)) a.push(v); }); | |
return a; | |
} | |
Array.prototype.map = function(mapper) | |
{ | |
var a = []; | |
this.each(function(v) { a.push(mapper(v)); }); | |
return a; | |
} | |
Math.even = function(v) { return v % 2 == 0; }; | |
Math.odd = function(v) { return !Math.even(v); }; | |
Math.mul = function(a, b) | |
{ | |
return arguments.length >= 2 ? a * b : arguments.callee.curry(a); | |
}; | |
Function.prototype.curry = function(first) | |
{ | |
var self = this; | |
return function() | |
{ | |
var args = [ first ]; | |
for (var i = 0; i < arguments.length; i++) | |
args.push(arguments[i]); | |
return self.apply(this, args); | |
}; | |
} | |
function alert(s) | |
{ | |
WScript.Echo(s); | |
} | |
function main() | |
{ | |
var i, args = [], range, filter, multiplier; | |
for (i = 0; i < WScript.Arguments.length; i++) | |
args.push(WScript.Arguments(i)); | |
if (args.length < 3) | |
{ | |
throw new Error('Missing required arguments. Usage is: RANGE ( "odd" | "even" ) MULTIPLIER'); | |
WScript.Quit(1); | |
} | |
range = Number(args.shift()); | |
if (isNaN(range)) | |
throw new Error('Invalid range.'); | |
filter = Math[args.shift()]; | |
if (!filter) | |
throw new Error("Invalid filter."); | |
multiplier = Number(args.shift()); | |
if (isNaN(multiplier)) | |
throw new Error('Invalid multiplier.'); | |
Array.range(range) | |
.filter(filter) | |
.map(Math.mul(multiplier)) | |
.each(alert); | |
} | |
try | |
{ | |
main(); | |
} | |
catch (e) | |
{ | |
alert(e.message); | |
WScript.Quit(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment