-
-
Save edisonywh/42b291d65476e9c60e34fb6557cb78a6 to your computer and use it in GitHub Desktop.
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 traverse(tail) | |
head = list.shift | |
puts "Element: #{head}, Stack Count: #{caller.count}" #=> new line | |
return if tail.empty? | |
traverse(tail) | |
end | |
# Normal ruby | |
traverse(large_array) | |
# Element: 1, Stack Count: 1 | |
# Element: ..., Stack Count: ... | |
# Element: ..., Stack Count: ... | |
# Element: 10918, Stack Count: 10918 | |
# => stack level too deep (SystemStackError) | |
# TCO-enabled | |
traverse(large_array) | |
# Element: 1, Stack Count: 1 | |
# Element: ..., Stack Count: 1 | |
# Element: ..., Stack Count: 1 | |
# Element: 500000, Stack Count: 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment