Created
September 14, 2012 01:33
-
-
Save jsmaker/3719278 to your computer and use it in GitHub Desktop.
Some nice Lisp concepts is Javascript
This file contains hidden or 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
Jsl() | |
( 'sum', 1, 2) | |
( Jsl.get, function(val){ this.firstSum = val; } ) | |
( Jsl.sum, 1, 2, 3, 4, 5 ) | |
( Jsl.mul, 1, 2, 3, 4, 5 ) | |
( Jsl.getAll, function(val1, val2) { | |
Jsl(this) | |
( Jsl.set, {secondSum : val1, thirdSum: val2} ) | |
(); | |
}) | |
(); |
This file contains hidden or 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
Jsl() | |
( Jsl.set, {name : 'barak'} ) | |
( Jsl.get, function(val){ this.name2 = val + '222222'; } ) | |
( function(){ this.someFunctionArgs = arguments; } , 'argument1', 'argument2') | |
( Jsl.log, 'this' ) | |
(Jsl.async, Jsl.ajax, '/app/get/bear') | |
(Jsl.async, Jsl.ajax, '/app/get/anther/bear') | |
(Jsl.async, Jsl.ajax, '/app/put/tip') | |
(Jsl.then, function(){ | |
Jsl(this) | |
( this.log, '_', arguments ) | |
(); | |
}) | |
(); |
This file contains hidden or 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
function Jsl(ctx) { | |
if ( !(this instanceof Jsl) && !(ctx instanceof Jsl) ) { return new Jsl(ctx); } | |
ctx = ctx || this; | |
var jslCallBuffer = []; | |
var jslCallReturnValues = []; | |
var jsl = function (action, param/*, args*/) { | |
action = typeof action === 'string' ? (ctx[action] || function(){}) : action; | |
if (action === Jsl.get) { return param.call(ctx, jslCallReturnValues.pop()); } | |
if (action === Jsl.getAll) { return param.apply(ctx, jslCallReturnValues) , jslCallReturnValues = []; } | |
jslCallReturnValues.push( action.apply( ctx, Jsl.getArgs(arguments, 1) ) ); | |
}; | |
var startOutput = function(){ | |
for (var i = 0; i < jslCallBuffer.length; i++) { | |
jsl.apply(ctx, jslCallBuffer[i]); | |
} | |
}; | |
var addToBuffer = function () { | |
if (!arguments.length) { return startOutput(); } | |
jslCallBuffer.push(arguments); | |
return addToBuffer; | |
}; | |
return addToBuffer; | |
} | |
Jsl.prototype = Jsl; | |
Jsl.get = function(){ | |
return this; | |
}; | |
Jsl.getAll = function(){ | |
return this; | |
}; | |
Jsl.getArgs = function (_arguments,start){ | |
return Array.prototype.slice.call(_arguments, start); | |
}; | |
Jsl.set = function(obj){ | |
for (var p in obj) { | |
this[p] = obj[p]; | |
} | |
}; | |
Jsl.prop = function(prop){ | |
if (prop === 'this') { | |
return this; | |
} | |
return this[prop]; | |
}; | |
Jsl.invoke = function(fn){ | |
fn.apply(this, Jsl.getArgs(arguments, 1)); | |
}; | |
Jsl.log = function(prop){ | |
var args = Jsl.getArgs(arguments, 1); | |
args.unshift('Jsl' + ': '); | |
//args.push((new Error).stack.replace('Error', '')); | |
if(Jsl.get.call(this, prop)){ args.push(Jsl.get.call(this, prop)); } | |
console.log.apply(console, args); | |
}; | |
Jsl.sum = function(){ | |
var a = 0; | |
for (var i = 0; i < arguments.length; i++) { | |
a += arguments[i]; | |
} | |
return a; | |
}; | |
Jsl.mul = function(){ | |
var a = arguments[0]; | |
for (var i = 1; i < arguments.length; i++) { | |
a *= arguments[i]; | |
} | |
return a; | |
}; | |
Jsl.async = function(fn){ | |
this.jslAsyncPromise = this.jslAsyncPromise || []; | |
this.jslAsyncPromise.push( fn.apply( this, Jsl.getArgs(arguments, 1) ) ); | |
return this.jslAsyncPromise; | |
}; | |
Jsl.then = function(fn){ | |
var allDone = this.jslAsyncPromise; | |
this.jslAsyncPromise = []; | |
Jsl.when(allDone).then(function(){ | |
fn.apply(this, arguments); | |
}); | |
}; | |
Jsl.ajax = function(url){ | |
var dummy = {url:url}; | |
return { | |
dummy:dummy, | |
then: function(){ | |
return fn.apply(ctx, [dummy]); | |
} | |
}; | |
}; | |
Jsl.when = function(allDone){ | |
var ctx = this; | |
var dummy = allDone.map(function(d){ | |
return d.dummy; | |
}); | |
return { | |
then: function(fn){ | |
return fn.apply(ctx, dummy); | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment