Last active
August 29, 2015 14:02
-
-
Save ww24/b529774f46056cd1d441 to your computer and use it in GitHub Desktop.
フィボナッチ数列 (Ruby の勉強)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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