さっくりTclのジェネレーターについて調べたので載せておく。
比較用にjsとVBもね。
以外に日本語資料がなかったから困った。
出力
0
1
2
3
4
5
6
7
8
9
10
Tcl
proc gen {} {
yield [info coroutine]
for {set i 0} {true} {incr i} {
yield $i
}
}
coroutine g gen
puts [g]
puts [g]
puts [g]
puts [g]
puts [g]
puts [g]
puts [g]
puts [g]
puts [g]
puts [g]
puts [g]
JavaScript
function* gen(){
for(let i=0;;i++){
yield i;
}
}
const g=gen();
console.log(g.next().value);
console.log(g.next().value);
console.log(g.next().value);
console.log(g.next().value);
console.log(g.next().value);
console.log(g.next().value);
console.log(g.next().value);
console.log(g.next().value);
console.log(g.next().value);
console.log(g.next().value);
console.log(g.next().value);
VB.Net
Imports System
Imports System.Console
Imports System.Collections.Generic
Module Program
Iterator Function gen() As IEnumerator(Of Integer)
For i As Integer=0 To Int32.MaxValue
Yield i
Next
End Function
Sub Main
Dim g=gen()
g.MoveNext() : WriteLine(g.Current)
g.MoveNext() : WriteLine(g.Current)
g.MoveNext() : WriteLine(g.Current)
g.MoveNext() : WriteLine(g.Current)
g.MoveNext() : WriteLine(g.Current)
g.MoveNext() : WriteLine(g.Current)
g.MoveNext() : WriteLine(g.Current)
g.MoveNext() : WriteLine(g.Current)
g.MoveNext() : WriteLine(g.Current)
g.MoveNext() : WriteLine(g.Current)
g.MoveNext() : WriteLine(g.Current)
End Sub
End Module