www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - construct range from tuple?

reply Lutger <lutger.blijdestijn gmail.com> writes:
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?
Sep 18 2016
next sibling parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
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"])
Try
   chain([names], ["Chuck"])
 [...]
Sep 18 2016
prev sibling parent reply e-y-e <yurtqbqn grr.la> writes:
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"]));
Sep 18 2016
next sibling parent e-y-e <yurtqbqn grr.la> writes:
On Sunday, 18 September 2016 at 09:36:13 UTC, e-y-e wrote:
 On Sunday, 18 September 2016 at 08:06:54 UTC, Lutger wrote:
 [...]
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"]));
[1] https://dlang.org/phobos/std_range.html#only
Sep 18 2016
prev sibling next sibling parent Jonathan M Davis via Digitalmars-d-learn writes:
On Sunday, September 18, 2016 09:36:13 e-y-e via Digitalmars-d-learn wrote:
 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"]));
You can also just stick them in an array. e.g. auto namesArr = [names]; But obviously, that allocates memory. So, whether using only or allocating an array would make more sense depends on what you want to do with the resulting range. - Jonathan M Davis
Sep 18 2016
prev sibling parent Lutger <lutger.blijdestijn gmail.com> writes:
On Sunday, 18 September 2016 at 09:36:13 UTC, e-y-e wrote:
 On Sunday, 18 September 2016 at 08:06:54 UTC, Lutger wrote:
 [...]
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"]));
That's *exactly* what I was looking for, thanx!
Sep 18 2016