Generators in D

Piotr Szturmaj bncrbme at jadamspam.pl
Tue May 17 07:37:05 PDT 2011


Piotr Szturmaj wrote:
> Hi,
>
> I've written some proof of concept code of generator pattern, example:
>
> void genSquares(out int result, int from, int to)
> {
> foreach (x; from .. to + 1)
> {
> yield!result(x * x);
> }
> }
>
> void main(string[] argv)
> {
> foreach (sqr; generator(&genSquares, 10, 20))
> writeln(sqr);
> }
>
> posted on github:
>
> https://github.com/pszturmaj/dgenerators
>
> Thanks to Sean Kelly who wrote Fiber code.
>
> Piotr

Well, I've just found this: 
http://www.mail-archive.com/digitalmars-d@puremagic.com/msg30204.html. 
It seems someone did it before ;)

In regards to serializable fibers, I've found some interesting potential 
usage - session handling in HTTP server. Fiber code could represent 
session and could be saved to disk in the middle of execution, just like 
sessions are saved in PHP. This is used to offload memory when handling 
great number of sessions.

If serialization will be clever enough, these fibers could even be 
shared across different servers! This is really a requirement in load 
balanced environments.

Piotr


More information about the Digitalmars-d mailing list