Generators.
Generators.
generator Fib(max: integer): integer; //returns a IEnumerable
var
i: integer;
last, current, temp: integer;
begin
last := 1;
current := 1;
if max >= 1 then
yield 1;
if max >= 2 then
yield 1;
for i := 3 to max do
begin
temp := last + current;
yield temp;
last := current;
current := temp;
end;
end;
generator Fib(max: integer): integer; //returns a IEnumerable
var
i: integer;
last, current, temp: integer;
begin
last := 1;
current := 1;
if max >= 1 then
yield 1;
if max >= 2 then
yield 1;
for i := 3 to max do
begin
temp := last + current;
yield temp;
last := current;
current := temp;
end;
end;
It's possible with my fiber implementation on windows (on other platforms I had to use a thread which performs not very well). So if someone has a clue about implementing coroutines on those, please contact me.
ReplyDeleteIt looks like this then:
function Fib(count: integer): IEnumerable;
begin
Result := Iterator(
procedure
var
i: integer;
last, current, temp: integer;
begin
last := 1;
current := 1;
if count >= 1 then
yield(current);
if count >= 2 then
yield(current);
for i := 3 to count do
begin
temp := last + current;
yield(temp);
last := current;
current := temp;
end;
end);
end;
Stefan Glienke Based on the discussions around Boost.Coroutine, I'd say that sounds like a messy job...
ReplyDeleteAsbjørn Heid Doesn't that sound like something you want to dig into? ;p Well when I originally came across that whole coroutine thing some years ago there were like 2 or 3 implementations out there that were handcrafted stacks and lots of assembler code. Fortunately Windows offers the Fiber API which does an excellent job for building OOP based coroutines ontop. But unfortunately that does not exist on other platforms.
ReplyDelete