D equivalent for LINQ Where
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Sun May 4 00:19:54 UTC 2025
On Saturday, May 3, 2025 12:08:45 PM Mountain Daylight Time Ali Çehreli via Digitalmars-d-learn wrote:
> On 5/2/25 2:44 PM, Python wrote:
> > On Friday, 2 May 2025 at 14:33:57 UTC, Richard (Rikki) Andrew Cattermole
> > wrote:
> >> Have you tried filter?
> >>
> >> https://dlang.org/phobos/std_algorithm_iteration.html#.filter
> >
> > Thx. Anyway, what find returns aftrr first occurrence is unexpected.
>
> Yes, understandably unexpected.
>
> However, the choice of returning a range solves the other design problem
> of what to do when no item is found. 'find' communicates that case with
> an empty range.
It's also far more useful in the general case, since you can always just use
front to get the specific element.
And really, when you consider how find with iterators typically works - i.e.
it returns an iterator to the element - having Phobos' find return the range
starting with that element is the equivalent behavior for ranges, since
ranges don't refer to individual elements. In both cases, find iterates
until it finds the element, and then it returns what it was iterating over
at that point in the iteration. It's just that with an iterator, you get
something that points to that individual element, whereas with a range, you
get a range of values which starts with that element. But it's certainly
true that the range equivalents of iterator-based algorithms can take some
getting used to.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list