D equivalent for LINQ Where

2025-05-02 Thread Python via Digitalmars-d-learn
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); } } ``` Out

Re: D equivalent for LINQ Where

2025-05-02 Thread Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
Have you tried filter? https://dlang.org/phobos/std_algorithm_iteration.html#.filter

Re: alias vs enum for lambdas?

2025-05-02 Thread Orion via Digitalmars-d-learn
Yes. But at the same time, defining a function via alias is a 1st class function, unlike a standard function definition! alias af = (int x) => x; auto sf(int x) => x; auto v = af; auto p = &sf; This means that the definitions are not equivalent. In essence, an alias function behaves like a la

Re: D equivalent for LINQ Where

2025-05-02 Thread Python via Digitalmars-d-learn
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.

Re: Array operations

2025-05-02 Thread monkyyy via Digitalmars-d-learn
On Friday, 2 May 2025 at 22:19:53 UTC, Andy Valencia wrote: In the following code, two questions. First, is there any difference between "x[] = y" and "x[] = y[]"? With basic slices no; I could construct something with overloads tho ```d import std.stdio : writeln; auto foo(ref int[] a, re

Re: D equivalent for LINQ Where

2025-05-02 Thread monkyyy via Digitalmars-d-learn
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;se

Re: alias vs enum for lambdas?

2025-05-02 Thread monkyyy via Digitalmars-d-learn
On Friday, 2 May 2025 at 13:55:46 UTC, Orion wrote: Yes. But at the same time, defining a function via alias is a 1st class function, unlike a standard function definition! alias af = (int x) => x; auto sf(int x) => x; auto v = af; auto p = &sf; This means that the definitions are not equival

Array operations

2025-05-02 Thread Andy Valencia via Digitalmars-d-learn
In the following code, two questions. First, is there any difference between "x[] = y" and "x[] = y[]"? It appears not. Second, in assigning from arrays of differing sizes, Phobos causes an illegal instruction, rather than the sort of exception I'd have expected. I'm curious why they steppe