Skip to content

Instantly share code, notes, and snippets.

@ww24
Last active August 29, 2015 14:02
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 ww24/b529774f46056cd1d441 to your computer and use it in GitHub Desktop.
Save ww24/b529774f46056cd1d441 to your computer and use it in GitHub Desktop.
フィボナッチ数列 (Ruby の勉強)
fibonacci = Enumerator.new do |y|
i = [0, 1]
loop do
y << i[0]
i = [i[1], i[0] + i[1]]
end
end
# 遅延評価
p fibonacci.lazy.map{|x| x ** 2}.take(20).force
def f(a0, a1)
Enumerator.new do |y|
a = [a0, a1]
loop {
y << a[0]
a = [a[1], yield(a[0], a[1])]
}
end
end
p f(0, 1){|a, b| a + b}.take(20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment