How to collect "some" items of a range in an array?

Renato renato at athaydes.com
Mon Jan 6 20:15:23 UTC 2025


On Monday, 6 January 2025 at 19:58:12 UTC, H. S. Teoh wrote:
> On Mon, Jan 06, 2025 at 07:30:27PM +0000, Renato via 
> Digitalmars-d-learn wrote:
>> Is there any Phobos function that collects "some" items of a 
>> range
>> into an array/slice?
>> It's kind of embarrassing that I've been trying to find this 
>> for hours
>> now without success :(.
> [...]
>> My use case is to parse a file format where a line is expected 
>> to have 4 items. I am currently using `takeExactly(4)` but 
>> that doesn't let me index on it, so I can only think of doing 
>> inconvenient (and inefficient) stuff like `front, 
>> skip(1).front ...` to avoid having to write my own `toArray` 
>> :D. I couldn't even find a method to take 1 item and advance 
>> the range! Am I just looking at the entirely wrong places 
>> (std.range, std.algorithm.iteration)??
> [...]
>
> What's wrong with just using std.array.array?
>
>
> T

So I was indeed looking at the wrong place. It's not very 
intuitive to me that a function that acts on ranges is not in 
std.range but in std.array.

I guess the idea is to keep std.range pure (all functions take 
and return ranges, not slices/arrays/HashMaps etc.)?


More information about the Digitalmars-d-learn mailing list