Skip to content

Instantly share code, notes, and snippets.

@will0
Created March 15, 2013 05:15
Show Gist options
  • Save will0/5167645 to your computer and use it in GitHub Desktop.
Save will0/5167645 to your computer and use it in GitHub Desktop.
these must be those fibers people are talking about
function pipeall(array) {
var chain = [];
var next;
var next_val = 0;
var tramp = function(result) {
var stage = chain[--next];
var fn;
if(next) {
fn = function() {
stage(result, tramp);
};
} else {
fn = function() {
stage(result);
next = chain.length;
if(next_val != array.length) {
tramp(array[next_val++]);
}
};
}
setTimeout(fn, 0);
};
var thru = function(stage) {
chain.push(stage);
return this;
};
var to = function(stage) {
chain.push(stage);
chain.reverse();
next = chain.length;
tramp(array[next_val++]);
};
return {
thru: thru,
to: to,
};
}
function pipe(value) {
return pipeall([value]);
}
function lift(fn) {
return function(arg, next) {
next(fn(arg));
};
}
function fizzbuzz(num) {
if(!(num % 15))
return "fizzbuzz";
if(!(num % 5))
return "buzz";
if(!(num % 3))
return "fizz";
return num;
}
function rangei(first, last) {
var vals = [];
while(first <= last) {
vals.push(first++);
}
return vals;
}
pipeall(rangei(1,17))
.thru(lift(fizzbuzz))
.to(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment