Skip to content

Instantly share code, notes, and snippets.

@Chouser
Created August 4, 2011 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Chouser/1126248 to your computer and use it in GitHub Desktop.
Save Chouser/1126248 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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