2
我需要寫一個斐波那契數列中的d發生器(從A,B開始), 我在這裏看到的例子:斐波納契發電機std.concurrency.Generator
https://dlang.org/library/std/concurrency/generator.html
import std.concurrency;
import std.stdio;
void main()
{
auto tid = spawn(
{
while (true)
{
writeln(receiveOnly!int());
}
});
auto r = new Generator!int(
{
foreach (i; 1 .. 10)
yield(i);
});
foreach (e; r)
{
tid.send(e);
}
}
但現在我不知道如何調用我寫的發電機:
import std.concurrency;
import std.stdio;
import std.conv;
import std.string;
void main()
{
writeln("Enter a then b");
auto a_str = readln.strip;
auto b_str = readln.strip;
int a = to!int(a_str,16);
int b = to!int(b_str,16);
auto tid = spawn(
{
while (true)
{
writeln(receiveOnly!int());
}
});
auto fib = new Generator!int(
{
yield(a);
yield(b);
while(true){
int temp = b;
b = a+b;
a = temp;
yield(b);
}
});
}
感謝。