Skip to content

Instantly share code, notes, and snippets.

@odf
Created August 23, 2011 00:06
Show Gist options
  • Save odf/1163976 to your computer and use it in GitHub Desktop.
Save odf/1163976 to your computer and use it in GitHub Desktop.
A tail-recursive factorial in Coffeescript with simulated tail-call optimization.
bounce = (val) -> val = val() while typeof val == 'function'; val
factorial = (n) ->
fact = (a, n) -> if n > 0 then -> fact a * n, n - 1 else a
bounce fact 1, n
console.log factorial 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment