Create a gist now

Instantly share code, notes, and snippets.

Demonstrate CPS for array processing
// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name cps-demo.js
// @formatting pretty_print
// ==/ClosureCompiler==
function forEach(coll, f) {
for(var i = 0; i < coll.length; ++i) {
f(coll[i]);
}
}
function cps_filter(f, x, next) { if(f(x)) next(x); }
function cps_map(f, x, next) { next(f(x)); }
function m1(x) { return x / 2; }
function f2(x) { return x % 2 == 0; }
function m3(x) { return x * 10; }
var input = [2, 4, 6, 8]
var a = [];
forEach(input, function(x1) {
cps_map(m1, x1, function(x2) {
cps_filter(f2, x2, function(x2) {
cps_map(m3, x2, function(x3) {
a.push(x3); })})})});
@Chouser
Owner
Chouser commented Aug 4, 2011

Example code for blog entry: http://blog.n01se.net/?p=362

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment