Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View 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

View 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]

View 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

View 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]

View 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

View 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

View 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