construct range from tuple?

e-y-e via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Sep 18 02:36:13 PDT 2016


On Sunday, 18 September 2016 at 08:06:54 UTC, Lutger wrote:
> I have a tuple of strings generated at compile time, for 
> example:
>
>   alias names = AliasSeq!("Alice", "Bob");
>
> How is it possible to construct a range of strings from this, 
> in order to use it at runtime with other range algorithms?
>
> For example, this
>
>   chain(names, ["Chuck"])
>
> doesn't work as intended because it expands to
>
>   chain("Alice", "Bob", ["Chuck"])
>
> I want some function makeRange that works like this:
>
> assert(chain(makeRange(names), ["Chuck"]).fold!( (x,y) => x ~ " 
> " ~ y) ==
>        "Alice Bob Chuck");
>
> What would be a good way to do that?

Use std.range's 'only' function [1], it takes variadic arguments 
of the same type and constructs a range consisting of them.

Example:

     import std.meta : AliasSeq;
     import std.range : only;
     import std.algorithm.comparison : equal;

     alias names = AliasSeq!("Alice", "Bob");

     auto range = only(names, "Chuck");
     assert(range.equal(["Alice", "Bob", "Chuck"]));


More information about the Digitalmars-d-learn mailing list