Jump to page: 1 2 3
Thread overview
Is there any reason why in can't work with dynamic arrays?
Sep 17, 2019
Brett
Sep 17, 2019
Adam D. Ruppe
Sep 17, 2019
Brett
Sep 18, 2019
Adam D. Ruppe
Sep 18, 2019
jmh530
Sep 18, 2019
Adam D. Ruppe
Sep 18, 2019
jmh530
Sep 18, 2019
Adam D. Ruppe
Sep 19, 2019
Brett
Sep 18, 2019
Daniel Kozak
Sep 19, 2019
Brett
Sep 18, 2019
Alex
Sep 19, 2019
Brett
Sep 17, 2019
Vladimir Panteleev
Sep 17, 2019
Patrick Schluter
Sep 17, 2019
Brett
Sep 18, 2019
Brett
Sep 18, 2019
Ali Çehreli
Sep 18, 2019
Brett
Sep 18, 2019
JN
Sep 18, 2019
Jonathan M Davis
September 17, 2019
T[] x;

if (y in x) ...

It just checks to see if y is in between 0 and length. Clearly y has to be an int and clearly the semantics are equivalent to AA and so it all works out rather than having an arbitrary special case.

This is helpful when one has generate algorithms that can work with AA's or DA's that use in.

September 17, 2019
On Tuesday, 17 September 2019 at 17:43:39 UTC, Brett wrote:
> T[] x;
>
> if (y in x) ...
>
> It just checks to see if y is in between 0 and length.

Most people asking for this want it to check that the *value* y is in the array x, which is rejected for requiring a linear search.

But your idea could be done quickly, just I expect it would cause confusion with the people asking for the other.

I like to just check if y >= 0 && y < x.length which is more clear  anyway.
September 17, 2019
On Tuesday, 17 September 2019 at 17:46:09 UTC, Adam D. Ruppe wrote:
> On Tuesday, 17 September 2019 at 17:43:39 UTC, Brett wrote:
>> T[] x;
>>
>> if (y in x) ...
>>
>> It just checks to see if y is in between 0 and length.
>
> Most people asking for this want it to check that the *value* y is in the array x, which is rejected for requiring a linear search.
>
> But your idea could be done quickly, just I expect it would cause confusion with the people asking for the other.
>
> I like to just check if y >= 0 && y < x.length which is more clear  anyway.


Yes, I could see that, but it doesn't do that with AA's so but I could see how it could be problematic.

Alternatively, have a keys for a DA:

then one can do

if (y in x.keys)

and this is far more expressive so there is no confusion and also works with AA's(and would not result in keys having to be created because it is semantically the same as (y in x) if x is an AA).




September 17, 2019
On Tuesday, 17 September 2019 at 17:46:09 UTC, Adam D. Ruppe wrote:
> On Tuesday, 17 September 2019 at 17:43:39 UTC, Brett wrote:
>> T[] x;
>>
>> if (y in x) ...
>>
>> It just checks to see if y is in between 0 and length.
>
> Most people asking for this want it to check that the *value* y is in the array x, which is rejected for requiring a linear search.
>
> But your idea could be done quickly, just I expect it would cause confusion with the people asking for the other.

It would definitely be very confusing to Python users, where "x in [a, b, c]" does check if the value is in the list using a linear search.

September 17, 2019
On Tuesday, 17 September 2019 at 17:43:39 UTC, Brett wrote:
> T[] x;
>
> if (y in x) ...
>
> It just checks to see if y is in between 0 and length. Clearly y has to be an int and clearly the semantics are equivalent to AA and so it all works out rather than having an arbitrary special case.
>
> This is helpful when one has generate algorithms that can work with AA's or DA's that use in.

This is the wrong forum for this kind of questions. Go to the learn forum.
September 17, 2019
On Tuesday, 17 September 2019 at 19:59:24 UTC, Patrick Schluter wrote:
> On Tuesday, 17 September 2019 at 17:43:39 UTC, Brett wrote:
>> T[] x;
>>
>> if (y in x) ...
>>
>> It just checks to see if y is in between 0 and length. Clearly y has to be an int and clearly the semantics are equivalent to AA and so it all works out rather than having an arbitrary special case.
>>
>> This is helpful when one has generate algorithms that can work with AA's or DA's that use in.
>
> This is the wrong forum for this kind of questions. Go to the learn forum.

Perhaps you need to go to the learn forum Boss?
September 18, 2019
On Tuesday, 17 September 2019 at 19:59:24 UTC, Patrick Schluter wrote:
> On Tuesday, 17 September 2019 at 17:43:39 UTC, Brett wrote:
>> T[] x;
>>
>> if (y in x) ...
>>
>> It just checks to see if y is in between 0 and length. Clearly y has to be an int and clearly the semantics are equivalent to AA and so it all works out rather than having an arbitrary special case.
>>
>> This is helpful when one has generate algorithms that can work with AA's or DA's that use in.
>
> This is the wrong forum for this kind of questions. Go to the learn forum.

Only if you go first Boss.
September 18, 2019
On Tuesday, 17 September 2019 at 18:10:40 UTC, Brett wrote:
> Alternatively, have a keys for a DA:
>
> then one can do
>
> if (y in x.keys)

Indeed.

Actually, fun fact: you can do that in your own code. Write a function `keys` that returns a struct implementing `opBinaryRight`.

Like so:

```
import std.stdio;

struct KeyRange {
	size_t max;
	bool opBinaryRight(string op : "in")(size_t v) {
		return v >= 0 && v < max; // the < 0 never passes but meh clearer code
	}
}

KeyRange keys(T)(T[] t) { return KeyRange(t.length); }
alias keys = object.keys; // also bring in the runtime library's keys for AA; this line lets both work together in the same module without name conflicts


void main()
{
	int[] x = [0, 2];
	int y = 1;
	writeln(y in x.keys); // works!
	writeln(y+5 in x.keys); // this too
}
```

The language features used there are UFCS (allowing x.keys), operator overloading (opBinaryRight), and overload set merging (the alias line with the comment). Searching those terms on this website should give more information if you'd like to learn more.
September 17, 2019
On 09/17/2019 06:19 PM, Brett wrote:

>> This is the wrong forum for this kind of questions. Go to the learn
>> forum.
>
> Only if you go first

In case it's not clear, the reason why these questions are better suited to the Learn forum is because many of us are hanging out over in that forum, hungry for answers to such questions.

Ali

September 18, 2019
On Tuesday, 17 September 2019 at 17:43:39 UTC, Brett wrote:
> T[] x;
>
> if (y in x) ...
>
> It just checks to see if y is in between 0 and length. Clearly y has to be an int and clearly the semantics are equivalent to AA and so it all works out rather than having an arbitrary special case.
>
> This is helpful when one has generate algorithms that can work with AA's or DA's that use in.

Wow! Does really in work like that with arrays? This is terrible. I'd never expect in to check if index is in range. In every language in existence (that I know of) that has 'in', 'in' is a search for existence of value, whether a linear search is required or not.
« First   ‹ Prev
1 2 3