Challenge Tuples

NotYouAgain NotYouAgain at gmail.com
Fri May 3 07:39:47 UTC 2024


On Friday, 3 May 2024 at 05:11:28 UTC, Salih Dincer wrote:
>
> ..
> Wouldn't it be great if there was a feature that worked at 
> runtime...
>
> SDB at 79

module m;
@safe:
private:
import std;

void main()
{
     auto myTuple = tuple(1, 2, 3, [1, 3], 5);
     int[] arrToSum;

     foreach(int i, val; myTuple.expand)
     {
         if(typeof(val).stringof == "int[]")
         {
             foreach(v; myTuple.expand[i..i+1])
                 arrToSum ~= v;
         }
         else
         {
             arrToSum ~= val;
         }
     }

     writefln("The total value of the tuples is: %s", 
arrToSum.sum); // 15
}



More information about the Digitalmars-d-learn mailing list