Skip to content

Instantly share code, notes, and snippets.

@francescoagati
Created November 10, 2012 02:41
Show Gist options
  • Save francescoagati/4049615 to your computer and use it in GitHub Desktop.
Save francescoagati/4049615 to your computer and use it in GitHub Desktop.
async map and filter using livescript and continuation.js
/* Generated by Continuation.js v0.0.5 */
(function () {
var async, slice$, result;
slice$ = [].slice;
async = function () {
var delay, sum, amap, afilter;
delay = function (fn) {
var i$, args, cb, wrapper;
args = 1 < (i$ = arguments.length - 1) ? slice$.call(arguments, 1, i$) : (i$ = 1, []), cb = arguments[i$];
wrapper = function () {
return cb(fn.apply(null, args));
};
return setTimeout(wrapper, 100);
};
sum = function (a, b) {
return a + b;
};
amap = function (list, fn, callback) {
var new_list, i$, len$, el, result;
new_list = [];
i$ = 0, len$ = list.length;
function loop_0(loop_0_cont) {
if (i$ < len$) {
el = list[i$];
delay(fn, el, function () {
result = arguments[0];
new_list.push(result);
++i$;
loop_0(loop_0_cont);
});
} else {
loop_0_cont();
}
}
loop_0(function () {
return callback(new_list);
});
};
afilter = function (list, fn, callback) {
var new_list, i$, len$, el, result;
new_list = [];
i$ = 0, len$ = list.length;
function loop_1(loop_1_cont) {
if (i$ < len$) {
el = list[i$];
delay(fn, el, function () {
result = arguments[0];
if (result) {
new_list.push(el);
}
++i$;
loop_1(loop_1_cont);
});
} else {
loop_1_cont();
}
}
loop_1(function () {
return callback(new_list);
});
};
return {
amap: amap,
afilter: afilter
};
}();
async.amap([
1,
2,
3,
4,
5
], function (it) {
return it + 10;
}, function () {
result = arguments[0];
console.log(result);
async.afilter(result, function (it) {
return it > 12;
}, function () {
result = arguments[0];
console.log(result);
});
});
}.call(this));
async= do ->
delay= (fn,...args,callback) ->
wrapper = -> callback(fn.apply(null,args))
setTimeout wrapper,100
sum = (a,b) -> a + b
amap= (list,fn,callback) ->
new_list=[]
for el in list
delay fn,el,cont(result)
new_list.push(result)
callback(new_list)
afilter= (list,fn,callback) ->
new_list=[]
for el in list
delay fn,el,cont(result)
new_list.push(el) if result
callback(new_list)
{
amap:amap
afilter:afilter
}
async.amap [1,2,3,4,5], (+ 10), cont(result)
console.log result
async.afilter result, (> 12), cont(result)
console.log result
var h, k, l;
k = [].slice;
var m;
m = function(c) {
var a, b, e;
b = 1 < (a = arguments.length - 1) ? k.call(arguments, 1, a) : (a = 1, []);
e = arguments[a];
setTimeout(function() {
return e(c.apply(null, b))
}, 100)
};
h = {b:function(c, a, b) {
function e(b) {
d < i ? (g = c[d], m(a, g, function(a) {
j = a;
f.push(j);
++d;
e(b)
})) : b()
}
var f, d, i, g, j;
f = [];
d = 0;
i = c.length;
e(function() {
return b(f)
})
}, a:function(c, a, b) {
function e(b) {
d < i ? (g = c[d], m(a, g, function(a) {
(j = a) && f.push(g);
++d;
e(b)
})) : b()
}
var f, d, i, g, j;
f = [];
d = 0;
i = c.length;
e(function() {
return b(f)
})
}};
h.b([1, 2, 3, 4, 5], function(c) {
return c + 10
}, function(c) {
l = c;
console.log(l);
h.a(l, function(a) {
return 12 < a
}, function(a) {
l = a;
console.log(l)
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment