2
2

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.

ジェネレータ(コルーチン・ファイバー・イテレータ)を色々な言語で

2
Last updated at Posted at 2018-02-10

JavaScript,C♯,VB,HSP,Python,D言語,Lua,Scheme(Gauche),Tcl,Rubyで
同様の処理を書き比べしました。

hoges=["hoge","fuga","piyo"]
foos=["foo","bar","baz"]

で定義されるリストを交互で順番に読み出します。

hoge
foo
fuga
bar
piyo
baz

JavaScript

"use strict";

function* gen(){
	const hoges=["hoge","fuga","piyo"];
	const foos=["foo","bar","baz"];
	for(var i=0,hlen=hoges.length;i<hlen;i=0|i+1){
		yield hoges[i];
		yield foos[i];
	}
}

(function(){
	const g=gen();
	for(var i=0;i<6;i=0|i+1){
		console.log(g.next().value);
	}
})();

C♯

using System;
using System.Collections.Generic;

class Program{
	static IEnumerator<string> gen(){
		string[] hoges={"hoge","fuga","piyo"};
		string[] foos={"foo","bar","baz"};
		for(var i=0;i<hoges.Length;i++){
			yield return hoges[i];
			yield return foos[i];
		}
	}

	static void Main(){
		var g=gen();
		for(var i=0;i<6;i++){
			g.MoveNext();
			Console.WriteLine(g.Current);
		}
	}
}

VB

Option Strict On
Option Infer On
Imports System
Imports System.Collections.Generic

Module Program
	Iterator Function gen() As IEnumerator(Of String)
		Dim hoges As String()={"hoge","fuga","piyo"}
		Dim foos As String()={"foo","bar","baz"}

		For i=0i To hoges.Length-1
			Yield hoges(i)
			Yield foos(i)
		Next
	End Function

	Sub Main()
		Dim g=gen()
		For i=1i To 6
			g.MoveNext()
			Console.WriteLine(g.Current)
		Next
	End Sub
End Module

HSP

# runtime "hsp3cl"
# cmpopt varinit 1

# module __gen value,_yield
	#define global gen(%1) dimtype %1,5 :newmod %1,__gen
	#modinit
		hoges="hoge","fuga","piyo"
		foos="foo","bar","baz"
		dimtype _yield,1
		newlab _yield,1: return
		for i,0,length(hoges)
			value=hoges(i)
			newlab _yield,1: return
			value=foos(i)
			newlab _yield,1: return
		next
		value=""
	return

	#modcfunc genNextValue
		gosub _yield: return value
# global

# module Program
	#deffunc main
		gen g
		repeat 6
			mes genNextValue(g)
		loop
	return
# global
main

Python

def gen():
	hoges=["hoge","fuga","piyo"]
	foos=["foo","bar","baz"]
	for i in range(0,len(hoges)):
		yield hoges[i]
		yield foos[i]

def main():
	g=gen()
	for i in range(0,6):
		print(next(g))

if __name__=="__main__":main()

D言語

import std.stdio;
import std.concurrency;

Generator!string gen(){
	return new Generator!string((){
	    yield("");
		auto hoges=["hoge","fuga","piyo"];
		auto foos=["foo","bar","baz"];
		for(auto i=0;i<hoges.length;i++){
			yield(hoges[i]);
			yield(foos[i]);
		}
    });
}

void main(){
	auto g=gen();
	for(int i=0;i<6;i++){
		g.popFront();
		writeln(g.front);
	}
}

Lua

function gen()
	return coroutine.create(function()
		local hoges={"hoge","fuga","piyo"}
		local foos={"foo","bar","baz"}
		for i=1,#hoges do
			coroutine.yield(hoges[i])
			coroutine.yield(foos[i])
		end
	end)
end

(function()
	local g=gen()
	for i=1,6 do
		print(({coroutine.resume(g)})[2])
	end
end)()

Scheme(Gauche)

(use gauche.generator)

(define gen
	(^()(generate(^(yield)
		(let loop(
			(hoges '("hoge" "fuga" "piyo"))
			(foos '("foo" "bar" "baz")
		))
			(yield (car hoges))
			(yield (car foos))
			(loop (cdr hoges) (cdr foos))
		)
	)))
)

((lambda()
	(let((g (gen)))
	(let loop ((i 0))
		(when (< i 6)
			(print (g))
			(loop (+ i 1))
		)
	))
))

Tcl

proc gen {g} {
	proc gen {} {
		set hoges {"hoge" "fuge" "piyo"}
		set foos {"foo" "bar" "baz"}
		yield [info coroutine]
		for {set i 0} {$i<[llength $hoges]} {incr i} {
			yield [lindex $hoges $i]
			yield [lindex $foos $i]
		}
	}
	coroutine $g gen
}

apply {{} {
	gen g
	for {set i 0} {$i<6} {incr i} {
		puts [g]
	}
}}

Ruby

def gen()
	Fiber.new do
		hoges=["hoge","fuga","piyo"]
		foos=["foo","bar","baz"]
		for i in 0..hoges.length-1 do
			Fiber.yield(hoges[i])
			Fiber.yield(foos[i])
		end
	end
end

lambda do
	g=gen()
	for i in 1..6 do
		puts g.resume
	end
end.[]
2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?