3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

tclのジェネレーター(コルーチン・イテレーター)

Posted at

さっくり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
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?