Created
August 14, 2013 04:45
-
-
Save wintercn/6228067 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
function run(code,callback){ | |
callback = callback || function(r){ | |
//console.log(r); | |
}; | |
function check(f){ | |
if(f instanceof Function) { | |
var f = f; | |
var args = code.slice(1,code.length); | |
args.push(callback); | |
f.apply(f,args); | |
return; | |
} | |
else callback(code[0]); | |
} | |
if(code[0] instanceof Array) { | |
run(code[0],check) | |
} | |
else if(code.length == 0) { | |
callback(code); | |
} | |
else check(code[0]); | |
} | |
function quote(){ | |
arguments[arguments.length-1](arguments[0] || []); | |
} | |
function parallel(args) { | |
var count = 0; | |
var callback = arguments[arguments.length-1]; | |
for(var i = 0; i<args.length; i++) { | |
void function(i) { | |
function check(r){ | |
args[i] = r; | |
if(++count < args.length) | |
return; | |
callback(args); | |
} | |
if(args[i] instanceof Array) | |
run(args[i],check); | |
else check(args[i]); | |
}(i) | |
} | |
} | |
function convert(f){ | |
var callback = arguments[arguments.length-1]; | |
callback(function(){ | |
var callback = arguments[arguments.length-1]; | |
var args = Array.prototype.slice.call(arguments); | |
args.pop(); | |
parallel(args,function(args) { | |
callback(f.apply(window,args)); | |
}); | |
}); | |
} | |
function method(object,name) { | |
parallel(arguments,function(){ | |
callback(function(){ | |
var args = Array.prototype.slice.call(arguments); | |
args.pop(); | |
arguments[arguments.length-1](object[name].apply(object,args)); | |
}); | |
}); | |
} | |
function timeout(t){ | |
setTimeout(arguments[arguments.length-1], t); | |
} | |
function event(eventTarget, type , useCapture){ | |
var callback = arguments[arguments.length-1]; | |
parallel(arguments,function(){ | |
if(! (typeof useCapture == "boolean")) { | |
useCapture = false; | |
} | |
var handler = function(event){ | |
eventTarget.removeEventListener(type, handler, useCapture); | |
callback(event); | |
} | |
eventTarget.addEventListener(type, handler, useCapture); | |
}); | |
} | |
function lambda(args,code) { | |
var callback = arguments[arguments.length-1]; | |
callback(function(){ | |
var callback = arguments[arguments.length-1]; | |
parallel(arguments,function(params) { | |
var argsIndex = {}; | |
for(var i = 0; i <args.length; i++) | |
{ | |
argsIndex[args[i]] = params[i]; | |
} | |
run(function replace(code){ | |
code = code.slice() | |
for(var i = 0; i < code.length; i++) { | |
if(code[i] instanceof Array) { | |
code[i] = replace(code[i]); | |
} | |
else if(argsIndex.hasOwnProperty(code[i])) { | |
code[i] = argsIndex[code[i]]; | |
} | |
} | |
return code | |
}(code),callback); | |
}) | |
}) | |
} | |
function label() { | |
var callback = arguments[arguments.length-1]; | |
parallel(arguments,function(args){ | |
var f = args[0]; | |
callback(function(){ | |
var callback = arguments[arguments.length-1]; | |
var args = Array.prototype.slice.call(arguments); | |
args.unshift(f); | |
callback(f.apply(window,args)); | |
}); | |
}); | |
} | |
///////////////////////////////////////////////////// | |
function plus1(x) { | |
return x+1; | |
} | |
function log(arg1) { | |
console.log(arg1); | |
} | |
run([ | |
[label,[lambda, ["self","x"],[ "self","self",[[convert,plus1],"x"], [timeout,1000],[[convert, log],"x"] ]]],1 | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment