Thread overview
D equivalent for LINQ Where
3 days ago
Python
3 days ago
Python
2 days ago
Ali Çehreli
3 days ago
monkyyy
3 days ago

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#.

3 days ago
Have you tried filter?

https://dlang.org/phobos/std_algorithm_iteration.html#.filter
3 days ago

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

3 days ago
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.
2 days ago
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

2 days ago
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