Skip to content

Instantly share code, notes, and snippets.

@Rosuav
Last active November 15, 2016 00:35
Show Gist options
  • Save Rosuav/48c414b08f0a30fdb9904285d121ec8b to your computer and use it in GitHub Desktop.
Save Rosuav/48c414b08f0a30fdb9904285d121ec8b to your computer and use it in GitHub Desktop.
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