Deciding one member of iteration chain at runtime

Ali Çehreli acehreli at yahoo.com
Fri Feb 17 17:42:19 UTC 2023


On 2/17/23 09:30, Chris Piker wrote:

 > operatorG needs
 > to be of one of two different types at runtime

std.range.choose may be useful but I think it requires creating two 
variables like g1 and g2 below:

import std.range;
import std.algorithm;

void main(string[] args) {
     const condition = (args.length > 1);

     // The first part of the algorithm
     auto r = iota(10)
              .filter!(n => n % 2);

     // Two different steps
     auto g1 = r.map!((int n) => n * n);
     auto g2 = r.map!((int n) => n * 10);

     // The rest of the algoritm
     auto result = choose(condition, g1, g2)
                   .array;
}

Ali



More information about the Digitalmars-d-learn mailing list