Tail-call optimization for Ruby
Go to file
Mike Cifelli 5959e9dc8d Update readme 2019-01-26 14:41:19 -05:00
.vscode Initial commit 2019-01-26 14:09:00 -05:00
lib Initial commit 2019-01-26 14:09:00 -05:00
test Update readme 2019-01-26 14:41:19 -05:00
README.md Update readme 2019-01-26 14:41:19 -05:00
mutual_recursion.gemspec Update readme 2019-01-26 14:41:19 -05:00

README.md

Mutual Recursion

Tail call optimization for mutually (and directly) recursive functions in Ruby.

The current desing uses a trampoline. However, it is implemented in a way that still allows a tail recursive function to return a Proc as its terminal value without it being called prematurely by accident.

examples

require 'mutual_recursion'

def mutual_one(x, y = 0)
  return terminal_value(y) if x.negative?

  tail_call { mutual_two(x, y + 1) }
end

def mutual_two(x, y)
  tail_call { mutual_one(x - 1, y) }
end

mutual_one(50_000).invoke
# => 50001
require 'mutual_recursion'

def direct(x, y = 0)
  return terminal_value(y) if x.negative?

  tail_call { direct(x - 1, y + 1) }
end

direct(50_000).invoke
# => 50001
require 'mutual_recursion'

def proc_returning(x, y = 0)
  return terminal_value(proc { "|#{y}|" }) if x.negative?

  tail_call { proc_returning(x - 1, y + 1) }
end

proc_returning(20).invoke.call
# => "|21|"