Skip to content

Instantly share code, notes, and snippets.

@yllan
Last active April 26, 2018 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yllan/733f036cf654761874e1578889c27832 to your computer and use it in GitHub Desktop.
Save yllan/733f036cf654761874e1578889c27832 to your computer and use it in GitHub Desktop.
/********** first version **********/
function double(x) {
return (x * 2);
}
double(double(1)); // => 4
/********** add log function! **********/
function double(x, log) {
return [(x * 2), (log + "calling double(" + x + ")!\n")];
}
var result_1 = double(1, "");
var result_2 = double(result_1[0], result_1[1]); // wtf?!
/********** functional pattern **********/
function make_loggable(x, log) {
return [x, log];
}
function chain(loggable, f) {
var r = f(loggable[0]);
return make_log(r[0], loggable[1] + r[1]);
}
function double(x) {
return make_log(x * 2, "calling double(" + x + ")!\n");
}
chain(chain(make_loggable(1, ""), double), double);
// with syntatic sugar, we can write something like: make_loggable(1, "") >>= double >>= double
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment