automate tuple creation

Steven Schveighoffer schveiguy at gmail.com
Fri Jan 21 18:50:46 UTC 2022


On 1/21/22 1:36 PM, H. S. Teoh wrote:
> On Fri, Jan 21, 2022 at 10:12:42AM +0000, forkit via Digitalmars-d-learn wrote:

> [...]
>>      // id needs to be 9 digits, and needs to start with 999
>>      int[] idArray = takeExactly(iota(999*10^^6, 10^^9),
>> recordsNeeded).array;
> [...]
> 
> This is wasteful if you're not planning to use every ID in this
> million-entry long array.  Much better to just use an AA to keep track
> of which IDs have already been generated instead.  Of course, if you
> plan to use most of the array, then the AA may wind up using more memory
> than the array. So it depends on your use case.

Yeah, iota is a random-access range, so you can just pass it directly, 
and not allocate anything.

Looking at the usage, it doesn't need to be an array at all. But 
modifying the code to properly accept the range might prove difficult 
for someone not used to it.

-Steve


More information about the Digitalmars-d-learn mailing list