compatible types for chains of different lengths

Brad Anderson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Nov 17 15:22:57 PST 2015


On Tuesday, 17 November 2015 at 22:47:17 UTC, Jon D wrote:
> I'd like to chain several ranges and operate on them. However, 
> if the chains are different lengths, the data type is 
> different. This makes it hard to use in a general way. There is 
> likely an alternate way to do this that I'm missing.
>
> [snip]
>
> Is there a different way to do this?
>
> --Jon

One solution:

import std.stdio;
import std.range;
import std.algorithm;

void main(string[] args)
{
     auto x1 = ["abc", "def", "ghi"];
     auto x2 = ["jkl", "mno", "pqr"];
     auto x3 = ["stu", "vwx", "yz"];
     auto chain1 = chain(x1, (args.length > 1) ? x2 : []);
     auto chain2 = chain(x1, x2, (args.length > 1) ? x3 : []);
     chain1.joiner(", ").writeln;
     chain2.joiner(", ").writeln;
}


More information about the Digitalmars-d-learn mailing list