How to collect "some" items of a range in an array?
monkyyy
crazymonkyyy at gmail.com
Mon Jan 6 20:03:06 UTC 2025
On Monday, 6 January 2025 at 19:30:27 UTC, Renato 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 :(.
>
> I think I know how to write this myself (though writing generic
> stuff on ranges is not exactly straightforward), but surely
> there is something for this and I am somehow missing it (even
> AI can't help me)?
>
> 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)??
```
import std;
alias takearray(int i)=(r)=>r.take(i).staticArray!i;
unittest{
iota(3).takearray!4.writeln;
iota(5).takearray!4.writeln;
}
```
More information about the Digitalmars-d-learn
mailing list