Thread overview
D equivalent for LINQ Where
May 02
Python
May 02
Python
May 02
monkyyy
May 02

I am trying to get some items from a list, but find gives me unexpected results.

import std.stdio;
import std.algorithm;

int[] x = [1, 2, 3, 4, 5, 1, 2, 3];

void main()
{
    auto selection = x.find!(a => a == 2);

    foreach(item;selection)
    {
        writeln(item);
    }
}

Output is 2, 3, 4, 5, 1, 2, 3

Basically, I am looking for Where from C#.

May 03
Have you tried filter?

https://dlang.org/phobos/std_algorithm_iteration.html#.filter
May 02

On Friday, 2 May 2025 at 14:32:04 UTC, Python wrote:

>

I am trying to get some items from a list, but find gives me unexpected results.

import std.stdio;
import std.algorithm;

int[] x = [1, 2, 3, 4, 5, 1, 2, 3];

void main()
{
    auto selection = x.find!(a => a == 2);

    foreach(item;selection)
    {
        writeln(item);
    }
}

Output is 2, 3, 4, 5, 1, 2, 3

Basically, I am looking for Where from C#.

https://hoogletranslate.com

I confirm, its filter

Find, drops until it finds the filter or element

May 02
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.
May 03
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.

Ali

May 03
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