Why tuples are not ranges?

Timoses timosesu at gmail.com
Thu Jun 28 14:46:10 UTC 2018


On Thursday, 28 June 2018 at 14:35:33 UTC, Mr.Bingo wrote:
> Seems like it would unify things quite a bit.
>
> import std.typecons, std.range, std.array, std.algorithm, 
> std.stdio;
>
> void main()
> {
> 	auto t = tuple(3,4,5,6);
> 	//auto t = [3,4,5,6];
> 	
> 	writeln(t.map!(a => 3*a).sum());
> 	
> 	
> }

You could make a range out of tuples. This seems to work:

     import std.typecons, std.range, std.array, std.algorithm, 
std.stdio;

     void main()
     {
         auto t = tuple(3,4,5,6);
         auto m = t.expand.only;
         writeln(m.map!(a => 3*a).sum());
     }

Tuple.expand will expand the elements when calling a function (in 
this case only from std.range).
https://dlang.org/library/std/typecons/tuple.expand.html


More information about the Digitalmars-d-learn mailing list