Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
4
Task/Anonymous-recursion/Ruby/anonymous-recursion-1.rb
Normal file
4
Task/Anonymous-recursion/Ruby/anonymous-recursion-1.rb
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
def fib(n)
|
||||
raise RangeError, "fib of negative" if n < 0
|
||||
(fib2 = proc { |m| m < 2 ? m : fib2[m - 1] + fib2[m - 2] })[n]
|
||||
end
|
||||
2
Task/Anonymous-recursion/Ruby/anonymous-recursion-2.rb
Normal file
2
Task/Anonymous-recursion/Ruby/anonymous-recursion-2.rb
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
(-2..12).map { |i| fib i rescue :error }
|
||||
=> [:error, :error, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
|
||||
4
Task/Anonymous-recursion/Ruby/anonymous-recursion-3.rb
Normal file
4
Task/Anonymous-recursion/Ruby/anonymous-recursion-3.rb
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
def fib(n)
|
||||
raise RangeError, "fib of negative" if n < 0
|
||||
(fib2 = proc { |n| n < 2 ? n : fib2[n - 1] + fib2[n - 2] })[n]
|
||||
end
|
||||
7
Task/Anonymous-recursion/Ruby/anonymous-recursion-4.rb
Normal file
7
Task/Anonymous-recursion/Ruby/anonymous-recursion-4.rb
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# Ruby 1.9
|
||||
(-2..12).map { |i| fib i rescue :error }
|
||||
=> [:error, :error, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
|
||||
|
||||
# Ruby 1.8
|
||||
(-2..12).map { |i| fib i rescue :error }
|
||||
=> [:error, :error, 0, 1, 0, -3, -8, -15, -24, -35, -48, -63, -80, -99, -120]
|
||||
5
Task/Anonymous-recursion/Ruby/anonymous-recursion-5.rb
Normal file
5
Task/Anonymous-recursion/Ruby/anonymous-recursion-5.rb
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
def fib(n)
|
||||
raise RangeError, "fib of negative" if n < 0
|
||||
Hash.new { |fib2, m|
|
||||
fib2[m] = (m < 2 ? m : fib2[m - 1] + fib2[m - 2]) }[n]
|
||||
end
|
||||
20
Task/Anonymous-recursion/Ruby/anonymous-recursion-6.rb
Normal file
20
Task/Anonymous-recursion/Ruby/anonymous-recursion-6.rb
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
require 'continuation' unless defined? Continuation
|
||||
|
||||
module Kernel
|
||||
module_function
|
||||
|
||||
def recur(*args, &block)
|
||||
cont = catch(:recur) { return block[*args] }
|
||||
cont[block]
|
||||
end
|
||||
|
||||
def recurse(*args)
|
||||
block = callcc { |cont| throw(:recur, cont) }
|
||||
block[*args]
|
||||
end
|
||||
end
|
||||
|
||||
def fib(n)
|
||||
raise RangeError, "fib of negative" if n < 0
|
||||
recur(n) { |m| m < 2 ? m : (recurse m - 1) + (recurse m - 2) }
|
||||
end
|
||||
32
Task/Anonymous-recursion/Ruby/anonymous-recursion-7.rb
Normal file
32
Task/Anonymous-recursion/Ruby/anonymous-recursion-7.rb
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
require 'continuation' unless defined? Continuation
|
||||
|
||||
module Kernel
|
||||
module_function
|
||||
|
||||
def function(&block)
|
||||
f = (proc do |*args|
|
||||
(class << args; self; end).class_eval do
|
||||
define_method(:callee) { f }
|
||||
end
|
||||
ret = nil
|
||||
cont = catch(:function) { ret = block.call(*args); nil }
|
||||
cont[args] if cont
|
||||
ret
|
||||
end)
|
||||
end
|
||||
|
||||
def arguments
|
||||
callcc { |cont| throw(:function, cont) }
|
||||
end
|
||||
end
|
||||
|
||||
def fib(n)
|
||||
raise RangeError, "fib of negative" if n < 0
|
||||
function { |m|
|
||||
if m < 2
|
||||
m
|
||||
else
|
||||
arguments.callee[m - 1] + arguments.callee[m - 2]
|
||||
end
|
||||
}[n]
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue