Allowing arbitrary types for a function's argument and return type
pineapple via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Oct 22 07:08:32 PDT 2015
On Thursday, 22 October 2015 at 13:58:56 UTC, Adam D. Ruppe wrote:
> D's templates are easy (you actually used one in there, the
> Generator is one!)
>
> Try this:
>
> import std.concurrency;
> Generator!T sequence(T)(T i){
> return new Generator!T({
> yield(i);
> while(i > 1){
> yield(i = (i % 2) ? (i * 3 + 1) : (i >> 1));
> }
> });
> }
Wonderful, thanks much! It keeps surprising me how easy it is to
do stuff like this.
More information about the Digitalmars-d-learn
mailing list