Created
May 10, 2011 20:36
-
-
Save vilmibm/965334 to your computer and use it in GitHub Desktop.
lispy js
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
// var myfun = defun(function () { | |
// // do something with/to this.args; | |
// }); | |
defun = function defun() { | |
var orig_func = arguments[0]; | |
return function() { | |
// convert the arguments object into a plain old array | |
var args = Array.prototype.slice.call(arguments); | |
return orig_func.apply({args:args}); | |
} | |
} | |
// 1 === car(1,2,3); | |
// 1 === car([1,2,3]); | |
car = defun(function() { | |
if (this.args.length === 1 && typeof(this.args[0]) === typeof([])) { | |
return this.args[0][0]; | |
} | |
else { | |
return this.args[0]; | |
} | |
}); | |
// [2,3] === cdr(1,2,3); | |
// [2,3] === cdr([1,2,3]); | |
cdr = defun(function() { | |
if (this.args.length === 1 && typeof(this.args[0]) === typeof([])) { | |
return this.args[0].splice(1); | |
} | |
else { | |
return this.args.splice(1); | |
} | |
}); | |
// [4,9,16] === map(function(x) { return x*x }, 2, 3, 4); | |
// [4,9,16] === map(function(x) { return x*x }, [2, 3, 4]); | |
map = defun(function() { | |
var func = car(this.args); | |
var args = cdr(this.args); | |
var collect = []; | |
var xs; | |
if (args.length === 1 && typeof(car(args)) === typeof([])) { | |
xs = car(args); | |
} | |
else { | |
xs = args; | |
} | |
for (key in xs) { | |
var x = xs[key]; | |
collect.push(func(x)); | |
} | |
return collect; | |
}); | |
console.log("> car(1,2,3)"); | |
console.log("< " + car(1,2,3)); | |
console.log("> cdr(1,2,3)"); | |
console.log("< " + cdr(1,2,3)); | |
console.log("> map(function(x) { x*x }, 2,3,4)"); | |
console.log("< " + map(function(x) {return x*x;}, 2,3,4)); | |
console.log("\n"); | |
console.log("> car([1,2,3])"); | |
console.log("< " + car([1,2,3])); | |
console.log("> cdr([1,2,3]"); | |
console.log("< " + cdr([1,2,3])); | |
console.log("> map(function(x) { x*x }, [2,3,4])"); | |
console.log("< " + map(function(x) {return x*x;}, [2,3,4])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment