SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.
SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.
Successfully reported this slideshow.
Activate your 14 day free trial to unlock unlimited reading.
57.
おまけ:内部イテレータとの比較
可変箇所が複数ならジェネレータのほうがよっぽどきれい
function fib(){ // 使い方
list($x, $y) = foreach(fib() as $x){
[0, 1]; // 終了条件
while (TRUE) { if ($x >= 100)
yield $x; break;
list($x, $y) = // ボディ部
[$y, $x+$y]; echo $x, "n";
} };
}
copyright(c) 2012 kuwata-lab.com all rights reserved.
58.
おまけ:内部イテレータとの比較
Rubyでは、1つの無名関数 (ブロック) で「終了条件」と
「ボディ部」の両方を指定できる。
def fib() // 使い方
x, y = 0, 1 fib {|x|
while true // 終了条件
yield x break if x >= 100
x, y = y, x+y // ボディ部
end puts x
end }
copyright(c) 2012 kuwata-lab.com all rights reserved.