Last active
April 26, 2018 05:09
-
-
Save yllan/733f036cf654761874e1578889c27832 to your computer and use it in GitHub Desktop.
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
/********** 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