Created
February 20, 2015 14:40
-
-
Save remcopeereboom/4efb34feb568f3e2c81e to your computer and use it in GitHub Desktop.
Enabling tail recursion in ruby
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
require_relative 'tail_call_optimization' # All files loaded AFTER this file with have tail-recursion enabled. | |
require_relative 'tail_factorial' |
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
def factorial(n, acc = 1) | |
return acc if n <= 1 | |
factorial(n - 1, n * acc) | |
end | |
puts factorial(10000) |
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
# Enable recursion GLOBALLY --> See my GitHub to do it locally (the right way). | |
RubyVM::InstructionSequence.compile_option = { | |
:tailcall_optimization => true, | |
:trace_instruction => false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment