Re: Adapting foreign iterators to D ranges

2024-04-24 Thread cc via Digitalmars-d-learn
On Wednesday, 24 April 2024 at 05:08:25 UTC, Salih Dincer wrote: Yes, `opApply()` works! You just need to use `do while()` instead of `while()` because it skips the first item. It depends on the type of structure being consumed, if it provides "next" as a direct pointer then yeah you would nee

Re: Adapting foreign iterators to D ranges

2024-04-23 Thread Salih Dincer via Digitalmars-d-learn
On Tuesday, 23 April 2024 at 06:02:18 UTC, cc wrote: Just to offer an alternative solution (since it sometimes gets overlooked), there is also the `opApply` approach. You don't get full forward range status, and checking whether it's empty essentially requires doing something like std.algorith

Re: Adapting foreign iterators to D ranges

2024-04-22 Thread cc via Digitalmars-d-learn
On Monday, 22 April 2024 at 11:36:43 UTC, Chloé wrote: I wish to adapt this interface to a forward range for use with foreach and Phobos' range utilities. This amounts to implementing empty, front, and popFront, in terms of next and some state. But there is a choice to be made regarding the fi

Re: Adapting foreign iterators to D ranges

2024-04-22 Thread Steven Schveighoffer via Digitalmars-d-learn
On Monday, 22 April 2024 at 11:36:43 UTC, Chloé wrote: The first implementation has the advantage is being simpler and empty being const, but has the downside that next is called even if the range ends up not being used. Is either approach used consistently across the D ecosystem? I always g

Re: Adapting foreign iterators to D ranges

2024-04-22 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Monday, 22 April 2024 at 11:36:43 UTC, Chloé wrote: The first implementation has the advantage is being simpler and empty being const, but has the downside that next is called even if the range ends up not being used. Is either approach used consistently across the D ecosystem? You can als

Adapting foreign iterators to D ranges

2024-04-22 Thread Chloé via Digitalmars-d-learn
Assume a third-party API of the following signature: T* next(I iter); which advances an iterator of sorts and returns the next element, or null when iteration is done. No other information about the state of the iterator is available. I wish to adapt this interface to a forward range for