automate tuple creation

Steven Schveighoffer schveiguy at gmail.com
Sat Jan 22 01:33:16 UTC 2022


On 1/21/22 6:24 PM, forkit wrote:
> On Friday, 21 January 2022 at 22:25:32 UTC, forkit wrote:
>>
> 
> I really like how alias and mixin can simplify my code even further:
> 
> //---
> 
> int[][int][] CreateDataSet
> (const(int) recordsNeeded, const(int) valuesPerRecord)
> {
>      int[][int][] records;
>      records.reserve(recordsNeeded);
> 
>      const int iotaStartNum = 100_000_001;
>      alias iotaValues = Alias!"iota(iotaStartNum, iotaStartNum + 
> recordsNeeded).enumerate";
>      alias recordValues = 
> Alias!"iota(valuesPerRecord).map!(valuesPerRecord => 
> cast(int)rnd.dice(0.6, 1.4)).array";

oof! use enums for compile-time strings ;)

```d
enum iotaValues = "iota(...";
```

> 
>      foreach(i, id; mixin(iotaValues))
>      {
>          records ~= [ id: mixin(recordValues) ];
>      }
> 
>      return records;
> }

Not sure I agree that the mixin looks better.

Also, I'm curious about this code:

```d
iota(valuesPerRecord).map!(valuesPerRecord => cast(int)rnd.dice(0.6, 
1.4)).array;
```

That second `valuesPerRecord` is not used in the lambda, and also it's 
not referring to the original element, it's the name of a parameter in 
the lambda.

Are you sure this is doing what you want?

-Steve


More information about the Digitalmars-d-learn mailing list