Skip to content

Instantly share code, notes, and snippets.

@alexrothenberg
Created March 13, 2013 14:18
Show Gist options
  • Save alexrothenberg/5152560 to your computer and use it in GitHub Desktop.
Save alexrothenberg/5152560 to your computer and use it in GitHub Desktop.
def fib(x)
puts "fib(#{x})"
return 1 if [0,1].include? x
return fib(x-1) + fib(x-2)
end
cache = (0..Float::INFINITY).lazy.map {|x| fib(x) }
class << cache
def [](n)
take(n).force.last
end
end
nth_element_from_list = lambda { |ary, n| ary[n]}
nth_fib = nth_element_from_list.curry[cache]
nth_fib.call(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment