[New DIP] add an 'in' operator for arrays#101
[New DIP] add an 'in' operator for arrays#101moon-chilled wants to merge 7 commits intodlang:masterfrom moon-chilled:master
Conversation
|
I understand the utility with the syntax similarity with Python. However, I'm skeptical of any built-in operator doing anything but In your example in the DIP, what should really be happening is a hash set (I know there isn't one in Phobos, but there are on DUB) should be used to store the elements and check for presence. Doing a linear search for every input is really costly. I know it's just an example, but IMO we shouldn't be making it more simple to do the wrong thing. |
|
Redblacktrees have |
|
FYI Andrei approved the in operator for iota recently: dlang/phobos#5629 (You might want to mention this precedent in your DIP) |
|
Difference with |
|
This proposal should probably remove AAs as an example because the
|
|
The upper time complexity bound for If we were to go with allowing int[] arr = [7, 1, 3, 2, 6, 2, 4];
int x = 2;
if (auto ptr = x in arr)
// prints: "found: 2, at position: 3"
writefln("found: %s, at position: %s", *ptr, ptr - arr.ptr);
else
assert (p is null); // not reached
assert (11 !in arr);I'd in favor of adding the |
|
The problem with |
|
@Elronnd Sorry for making you wait so long. Thanks for your patience! With the new procedures approved, I'm starting up the DIP process again. This one is second in line, so if you are still willing to push it through the process (and I hope you are), I ask that you familiarize yourself with the new procedure document. I've got a blog post coming in a few days that will explain the motivation for the changes. I also ask that you make sure the document conforms to the new template. Just some minor structural changes. We'll start making progress on your DIP as soon as I know what the status is of the one in front of yours. |
|
@Elronnd You're next up! If you want to get started, I need to know soon, otherwise I'll have to move on to the next in line. |
|
Hi, sorry for the delay, I've started working on the next version. It should be ready today or tomorrow. If it's more convenient to, then by all means, do the next one in line first. |
|
Alright then. I'll come back and look at this again when you've got your new version ready. Thanks! |
|
@Elronnd Is this ready to start moving forward? |
|
Yep! |
|
In the interest of time - unless this DIP presents a fresh argument/angle and has broad support, it's quite unlikely it will get approved. |
|
That sounds reasonable. I'm closing this now, and will probably make a new dip in the near future. |
|
|
||
| The `in` operator for arrays will take a needle, of type `T`, and a haystack, | ||
| of type `T[]`. If any of the elements of the haystack are equal to the needle, | ||
| then it will return true. Otherwise, it will return false. No new grammar |
There was a problem hiding this comment.
I think the right signature would be:
inout(T)* opIn_impl(T)(inout(T)[] haystack, auto ref const(T) needle)
if (__traits(compiles, { bool ok = haystack[0] == needle; }))This is would be consistent with testing membership in AAs and will allow easy use of the found element, if any.
Example implementation: https://run.dlang.io/is/elciqv
Edit: sorry for the noise, I didn't read the comments and forgot that I even reviewed this DIP :O
There was a problem hiding this comment.
I think that returning a pointer is a bad idea since array values aren't guaranteed to be unique, so if I do 5 in [5, 6, 5, 4], I get a pointer only to the first 5. A solution could be to return an array of pointers or (I prefer this) an array of indices. Both of those have the advantage that arrays convert implicitly to bool (which might be a bad thing but that's a separate discussion), meaning that if (5 in arr) will still work.
There was a problem hiding this comment.
What's wrong with simply returning a pointer to the first occurrence, or returning an advanced slice, ala std.algorithm.find?
No description provided.