Thread overview
[Issue 11252] "in" operator for std.range.iota
Aug 22, 2015
Jack Stouffer
Sep 03, 2015
Jack Stouffer
Sep 03, 2015
ag0aep6g@gmail.com
Sep 04, 2015
Jonathan M Davis
Dec 25, 2019
berni44
August 22, 2015
https://issues.dlang.org/show_bug.cgi?id=11252

Jack Stouffer <jack@jackstouffer.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jack@jackstouffer.com

--- Comment #1 from Jack Stouffer <jack@jackstouffer.com> ---
This enhancement request makes no sense, as the "in" operator in Python and D do two completely different things.

To replicate the Python behavior, you can do the following:

import std.stdio, std.range, std.algorithm.searching;

void main() {
    if (iota(1, 10).countUntil(foo(2)) > -1) {
        "yes".writeln;
    }
}

But I don't see why you would want to, as this is much faster:

void main() {
    immutable int temp = foo(2);
    if (temp >= 1 && temp <= 10) {
        "yes".writeln;
    }
}

--
September 03, 2015
https://issues.dlang.org/show_bug.cgi?id=11252

Jack Stouffer <jack@jackstouffer.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |INVALID

--
September 03, 2015
https://issues.dlang.org/show_bug.cgi?id=11252

ag0aep6g@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
                 CC|                            |ag0aep6g@gmail.com
         Resolution|INVALID                     |---

--- Comment #2 from ag0aep6g@gmail.com ---
(In reply to Jack Stouffer from comment #1)
> This enhancement request makes no sense, as the "in" operator in Python and D do two completely different things.
> 
> To replicate the Python behavior, you can do the following:
> 
> import std.stdio, std.range, std.algorithm.searching;
> 
> void main() {
>     if (iota(1, 10).countUntil(foo(2)) > -1) {
>         "yes".writeln;
>     }
> }
> 
> But I don't see why you would want to, as this is much faster:
> 
> void main() {
>     immutable int temp = foo(2);
>     if (temp >= 1 && temp <= 10) {
>         "yes".writeln;
>     }
> }

The request is not for Python's `in`. The request is for a succinct way to do what Python's `1 < foo(2) < 10` does. The suggested solution with D's `in` would of course not do a linear search. Reopening.

--
September 04, 2015
https://issues.dlang.org/show_bug.cgi?id=11252

Jonathan M Davis <issues.dlang@jmdavisProg.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |issues.dlang@jmdavisProg.co
                   |                            |m

--- Comment #3 from Jonathan M Davis <issues.dlang@jmdavisProg.com> ---
in must be no worse than O(log n) or it is inappropriate to use it. And given how iota works, that can't possibly be done in anything better than O(n) in the general case, though it would be possible to implement it for integers specifically. Honestly though, this seems like an abuse of the in operator to me. in is really intended for lookup in containers. It's definitely not a range operation.

A better alternative would be to add find/canFind as a member function to iota in the cases where it would be more efficient than the std.algorithm implementation. Then any code which uses find/canFind with UFCS with iota and integral values would get a performance boost (including in generic code) rather than just this specific case where the code is specifically written for iota.

--
September 18, 2015
https://issues.dlang.org/show_bug.cgi?id=11252

--- Comment #4 from bearophile_hugs@eml.cc ---
(In reply to Jonathan M Davis from comment #3)

> Honestly though, this seems like an abuse of the in
> operator to me. in is really intended for lookup in containers. It's
> definitely not a range operation.

There's a very nice "in" operator in D, that most other languages miss, and it's quite under-used, mostly for irrational reasons.


> in must be no worse than O(log n) or it is inappropriate to use it. And given how iota works, that can't possibly be done in anything better than O(n) in the general case, though it would be possible to implement it for integers specifically.

I agree. One solution is to not compile the "in" if the search is slower than O(log n), but it doesn't sound very nice.


> A better alternative would be to add find/canFind as a member function to iota

Using "in" for ranges like iota is more natural (here "natural" means that it requires the programmer to remember less things) than using find/canFind, but they seem acceptable.

--
August 03, 2017
https://issues.dlang.org/show_bug.cgi?id=11252

--- Comment #5 from github-bugzilla@puremagic.com ---
Commits pushed to master at https://github.com/dlang/phobos

https://github.com/dlang/phobos/commit/80d1e84453e5a8c20ef53fd459ce641ca8df0d4e Issue 11252 - In operator requested for std.range.iota

https://github.com/dlang/phobos/commit/017ca067e668546a5df0d5b45442e44ee225f266 Merge pull request #5629 from dukc/11252

Issue 11252 - In operator requested for std.range.iota merged-on-behalf-of: Jack Stouffer <jack@jackstouffer.com>

--
August 16, 2017
https://issues.dlang.org/show_bug.cgi?id=11252

--- Comment #6 from github-bugzilla@puremagic.com ---
Commits pushed to stable at https://github.com/dlang/phobos

https://github.com/dlang/phobos/commit/80d1e84453e5a8c20ef53fd459ce641ca8df0d4e Issue 11252 - In operator requested for std.range.iota

https://github.com/dlang/phobos/commit/017ca067e668546a5df0d5b45442e44ee225f266 Merge pull request #5629 from dukc/11252

--
January 05, 2018
https://issues.dlang.org/show_bug.cgi?id=11252

--- Comment #7 from github-bugzilla@puremagic.com ---
Commits pushed to dmd-cxx at https://github.com/dlang/phobos

https://github.com/dlang/phobos/commit/80d1e84453e5a8c20ef53fd459ce641ca8df0d4e Issue 11252 - In operator requested for std.range.iota

https://github.com/dlang/phobos/commit/017ca067e668546a5df0d5b45442e44ee225f266 Merge pull request #5629 from dukc/11252

--
December 25, 2019
https://issues.dlang.org/show_bug.cgi?id=11252

berni44 <bugzilla@d-ecke.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
                 CC|                            |bugzilla@d-ecke.de
         Resolution|---                         |FIXED

--