www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - D equivalent for LINQ Where

reply Python <python python.com> writes:
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


May 02
next sibling parent reply "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
Have you tried filter?

https://dlang.org/phobos/std_algorithm_iteration.html#.filter
May 02
parent reply Python <python python.com> writes:
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 02
parent reply =?UTF-8?Q?Ali_=C3=87ehreli?= <acehreli yahoo.com> writes:
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
parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
May 03
prev sibling parent monkyyy <crazymonkyyy gmail.com> writes:
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


https://hoogletranslate.com I confirm, its filter Find, drops until it finds the filter or element
May 02