Skip to content

Instantly share code, notes, and snippets.

@dsshap
Last active September 11, 2015 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsshap/4afb9e4eaff2ed0949c5 to your computer and use it in GitHub Desktop.
Save dsshap/4afb9e4eaff2ed0949c5 to your computer and use it in GitHub Desktop.
def acquire_values_recursively(list_node, val_array=[])
return [] if list_node.nil?
val_array << list_node.value
unless list_node.next_node.nil?
acquire_values_recursively(list_node.next_node, val_array)
end
return val_array
end
- compared to -
def acquire_values_recursively(list_node, val_array=[])
return [] if list_node.nil?
if list_node.next_node.nil?
val_array << list_node.value
return val_array
else
val_array << list_node.value
acquire_values_recursively(list_node.next_node, val_array)
end
return val_array
end
@azurewraith
Copy link

Nice, I like the optimization. Thanks!

The only reason I kept it in there is because it throws a stack error if there is an infinite list to match a test. I should have thrown the test out, but sometimes bubbling a payload up the stack with desired ordering is tricky and nice to demonstrate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment