6. Pythonだったら:
for i in range(10):
print I love. you.
(おまけ)
Scalaだったら:
class IntUtil(n:Int) {
def times(block: => Unit)= {
for(x <- 1 to n) {
block
}
n
}
}
implicit def int2Times(n:Int):IntUtil = {new IntUtil(n)}
10.times{println("hello")}
6
Kazumi007の日記
http://d.hatena.ne.jp/Kazumi007/20090405
7. Simula I and 67
Ole-Johan Dahl and Kristen Nygaard
(1962-1967, ノルウェー計算センター)
オブジェクト、 クラス、サブクラス、継承、動的束縛
(仮想関数)、コルーチン、ガベージコレクション
7
OOの歴史
8. 8
Begin
Class Glyph;
Virtual: Procedure print Is Procedure print;
Begin
End;
Glyph Class Char (c);
Character c;
Begin
Procedure print;
OutChar(c);
End;
Glyph Class Line (elements);
Ref (Glyph) Array elements;
Begin
Procedure print;
Begin
Integer i;
For i:= 1 Step 1 Until UpperBound (elements, 1) Do
elements (i) .print;
OutImage;
End;
End;
Ref (Glyph) rg ;
Ref (Glyph) Array rgs (1:4);
! Main program;
rgs (1):- New Char ('A');
rgs (2):- New Char ('b');
rgs (3):- New Char ('b');
rgs (4):- New Char ('a');
rg:- New Line (rgs);
rg.print; ! Virtual method call
End;
Simula
http://ja.wikipedia.org/wiki/Simula
18. 18
既存クラスの「動的拡張」
3.times {puts 'I love you.'}
class Fixnum # “Open” existing class.
def my_times
i = self
while i > 0
i = i - 1
print "["
yield
print "]n"
end
end
end
puts 3.class
puts 3.my_times {print 'I love you.'}
(実行例)
I love you.
I love you.
I love you.
Fixnum
[I love you.]
[I love you.]
[I love you.]
19. 19
Duck Typing
"If it walks like a duck and quacks like a duck, it
must be a duck"
もしあるオブジェクトがpush, popメソッドを提供する
なら,それはスタックとみなしてよい。
…厳密にduck, スタックであるかどうかを問わ
ない。duck, スタックである様であればよい。