Created
August 4, 2011 20:53
-
-
Save Chouser/1126248 to your computer and use it in GitHub Desktop.
Demonstrate CPS for array processing
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
// ==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); })})})}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example code for blog entry: http://blog.n01se.net/?p=362