Created
September 27, 2016 07:04
-
-
Save pocari/031a6da96c066badc5fe3abb62fab15b to your computer and use it in GitHub Desktop.
ruby's tailcall optimization
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
RubyVM::InstructionSequence.compile_option = { | |
tailcall_optimization: true, | |
trace_instruction: false | |
} | |
RubyVM::InstructionSequence.compile(<<EOS).eval | |
def factorial(n, acc = 1) | |
if n == 0 | |
acc | |
else | |
factorial(n - 1, acc * n) | |
end | |
end | |
EOS | |
p factorial(100000).to_s.size |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment