digitalmars.D.learn - D equivalent for LINQ Where
- Python (17/17) May 02 I am trying to get some items from a list, but `find` gives me
- Richard (Rikki) Andrew Cattermole (2/2) May 02 Have you tried filter?
- Python (4/6) May 02 Thx. Anyway, what find returns aftrr first occurrence is
- =?UTF-8?Q?Ali_=C3=87ehreli?= (6/12) May 03 Yes, understandably unexpected.
- Jonathan M Davis (14/26) May 03 It's also far more useful in the general case, since you can always just...
- monkyyy (4/21) May 02 https://hoogletranslate.com
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
Have you tried filter? https://dlang.org/phobos/std_algorithm_iteration.html#.filter
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#.filterThx. Anyway, what find returns aftrr first occurrence is unexpected.
May 02
On 5/2/25 2:44 PM, Python wrote:On Friday, 2 May 2025 at 14:33:57 UTC, Richard (Rikki) Andrew Cattermole wrote: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. AliHave you tried filter? https://dlang.org/phobos/std_algorithm_iteration.html#.filterThx. Anyway, what find returns aftrr first occurrence is unexpected.
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
May 03
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, 3https://hoogletranslate.com I confirm, its filter Find, drops until it finds the filter or element
May 02