Is there such concept of a list in D?

Salih Dincer salihdb at hotmail.com
Sun Dec 11 07:47:35 UTC 2022


On Sunday, 11 December 2022 at 03:13:17 UTC, zjh wrote:
> On Saturday, 10 December 2022 at 19:49:23 UTC, Salih Dincer 
> wrote:
>>
>> SDB at 79
>
>
> Can the `range` be partially traversed? That is, suppose we 
> only access the `[3,5)` elements?

Certainly, there are many ways to do this. For example:

```d
   //...
   myList1.skipRange(3).rangePrint; /*
     1: Sivrihisar
     2: Shemseddin
     3: Nasruddin
     4: Nusrat
   */
   myNames[3..5].rangePrint; /*
     1: Sivrihisar
     2: Shemseddin
   */
}

auto skipRange(R)(R range, size_t value)
{
   size_t loop = value;
   while(!range.empty && loop--)
   {
     range.popFront();
   }
   return range;
}

void rangePrint(R)(R range)
{
   import std.stdio;
   size_t i = 1;
   foreach(item; range)
   {
     writeln(i++, ": ", item);
   }
   writeln;
}
```

SDB at 79


More information about the Digitalmars-d-learn mailing list