Last active
November 15, 2016 00:35
-
-
Save Rosuav/48c414b08f0a30fdb9904285d121ec8b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function triangle(x) | |
{ | |
if (x <= 1) | |
{ | |
if (x == 1) return 1; | |
else throw "Can't get triangle number of " + x; | |
} | |
return triangle(x - 1) + x; | |
} | |
function triangle_callback(x, cb) | |
{ | |
if (x <= 1) | |
{ | |
if (x == 1) cb(1); | |
else throw "Can't get triangle number of " + x; | |
} | |
else triangle_callback(x - 1, function(tri) {cb(tri + x);}); | |
} | |
function triangle_promise(x) | |
{ | |
if (x <= 1) | |
{ | |
return new Promise(function(resolve, reject) { | |
if (x == 1) resolve(1); | |
else throw "Can't get triangle number of " + x; | |
}); | |
} | |
return triangle_promise(x - 1).then(tri => tri + x); | |
} | |
function* triangle_gen(x) | |
{ | |
if (x <= 1) | |
{ | |
if (x == 1) return 1; | |
else throw "Can't get triangle number of " + x; | |
} | |
yield triangle_gen(x - 1).next().value + x; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment