parameter pack to inputRange

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 5 22:32:43 PDT 2016


On 05/05/2016 10:00 PM, Erik Smith wrote:
> Is there an existing way to adapt a parameter pack to an input range? I
> would like to construct an array with it.  Example:
>
> void run(A...) (A args) {
>       Array!int a(toInputRange(args));
> }
>

Inspired by my DConf 2016 talk ;) here is a fiber-based InputRange solution:

import std.stdio;
import std.concurrency;
import std.container;

alias GeneratorFiber = std.concurrency.Generator;

void run(A...) (A args) {
     void asInputRange() {
         /* static */ foreach (arg; args) {
             yield(arg);
         }
     }

     auto range =  new GeneratorFiber!int(&asInputRange);
     auto a = Array!int(range);
     writeln(a[]);
}

void main() {
     run(1, 2, 3);
}

Ali



More information about the Digitalmars-d-learn mailing list