digitalmars.D.learn - myrange.at(i) for myrange.dropExactly(i).front
- Timothee Cour via Digitalmars-d-learn (5/5) Jul 25 2014 Is there a function for doing this?
- Jonathan M Davis (6/12) Jul 25 2014 That would require a random access range, in which case you can
- Ary Borenszweig (5/16) Jul 25 2014 No, the OP said the meaning was `myrange.dropExactly(i).front`, which is...
- Jonathan M Davis (25/46) Jul 25 2014 That is an inherently expensive operation, so it would be a very
- monarch_dodra (8/12) Jul 26 2014 What he did also say is he wanted the equivalent of C++'s "at",
- Timothee Cour via Digitalmars-d-learn (10/24) Jul 27 2014 Just for clarification, I wanted 'myrange.at(i)' to be the same as
- H. S. Teoh via Digitalmars-d-learn (12/24) Jul 27 2014 [...]
- Timothee Cour via Digitalmars-d-learn (4/30) Jul 29 2014 Obviously I did that, but I thought it belonged in phobos. Anyway, closi...
Is there a function for doing this? myrange.at(i) (with meaning of myrange.dropExactly(i).front) it's a common enough operation (analog to myrange[i]; the naming is from C++'s std::vector<T>::at)
Jul 25 2014
On Friday, 25 July 2014 at 21:33:23 UTC, Timothee Cour via Digitalmars-d-learn wrote:Is there a function for doing this? myrange.at(i) (with meaning of myrange.dropExactly(i).front) it's a common enough operation (analog to myrange[i]; the naming is from C++'s std::vector<T>::at)That would require a random access range, in which case you can just index directly. For a non-random access range, which you're doing would be the most direct way of doing it. - Jonathan M Davis
Jul 25 2014
On 7/25/14, 6:39 PM, Jonathan M Davis wrote:On Friday, 25 July 2014 at 21:33:23 UTC, Timothee Cour via Digitalmars-d-learn wrote:No, the OP said the meaning was `myrange.dropExactly(i).front`, which is not a random access. Sometimes you *do* want the n-th element of a range even if the range is not a random access.Is there a function for doing this? myrange.at(i) (with meaning of myrange.dropExactly(i).front) it's a common enough operation (analog to myrange[i]; the naming is from C++'s std::vector<T>::at)That would require a random access range, in which case you can just index directly. For a non-random access range, which you're doing would be the most direct way of doing it. - Jonathan M Davis
Jul 25 2014
On Saturday, 26 July 2014 at 00:28:32 UTC, Ary Borenszweig wrote:On 7/25/14, 6:39 PM, Jonathan M Davis wrote:That is an inherently expensive operation, so it would be a very bad idea IMHO to support it. The OP referenced vector, which has random access, and that's a completely different ballgame. In general, when operating on ranges, you should be trying to iterate over them only once and to backtrack as little as possible if you have backtrack. It's true that's not always possible, but if at() were O(n), then it would make inefficient code less obvious. I'd argue against at() working on non-random access ranges for the same reason that std.container doesn't support containers with a length property of O(n) - because it's a function that looks like it's O(1), and programmers will consistently think that it's O(1) and misuse it. C++ has had that problem with std::list' size function which is O(n). at() looks like it would be O(1) (and it always is in C++), so it would be inappropriate to have it in cases where it would need to be O(n), and since we already have [], why add at()? It exists on vector in addition to [] to give it range checking random-access. We already have that in D with []. myrange.dropExactly(i).front makes it much more obvious what you're doing and that it's inefficient. It might be necessary in some cases, but we don't want to give the impression that it's cheap, which at() would do. - Jonathan M DavisOn Friday, 25 July 2014 at 21:33:23 UTC, Timothee Cour via Digitalmars-d-learn wrote:No, the OP said the meaning was `myrange.dropExactly(i).front`, which is not a random access. Sometimes you *do* want the n-th element of a range even if the range is not a random access.Is there a function for doing this? myrange.at(i) (with meaning of myrange.dropExactly(i).front) it's a common enough operation (analog to myrange[i]; the naming is from C++'s std::vector<T>::at)That would require a random access range, in which case you can just index directly. For a non-random access range, which you're doing would be the most direct way of doing it. - Jonathan M Davis
Jul 25 2014
On Saturday, 26 July 2014 at 00:28:32 UTC, Ary Borenszweig wrote:No, the OP said the meaning was `myrange.dropExactly(i).front`, which is not a random access. Sometimes you *do* want the n-th element of a range even if the range is not a random access.What he did also say is he wanted the equivalent of C++'s "at", which is the equivalent of "checked random-access" (or "checked dictionary access"). So the actual requirements aren't very clear. In terms of "C++ at" equivalent, I don't think we have anything equivalent to offer. That said, I've never seen anyone use "at" in C++ ever.
Jul 26 2014
Just for clarification, I wanted 'myrange.at(i)' to be the same as `myrange.dropExactly(i).front` (so I don't assume it's a random access range).doing and that it's inefficient. It might be necessary in some cases, but we don't want to give the impression that it's cheap, which at() would do. I think it's already clear that it's potentially O(n) [n=i] cost as we're not using myrange[i]. But fine, call it atWalk/walkAt/whatever. Point is it's a common enough operation. On Sat, Jul 26, 2014 at 11:15 AM, monarch_dodra via Digitalmars-d-learn < digitalmars-d-learn puremagic.com> wrote:myrange.dropExactly(i).front makes it much more obvious what you'reOn Saturday, 26 July 2014 at 00:28:32 UTC, Ary Borenszweig wrote:No, the OP said the meaning was `myrange.dropExactly(i).front`, which is not a random access. Sometimes you *do* want the n-th element of a range even if the range is not a random access.What he did also say is he wanted the equivalent of C++'s "at", which is the equivalent of "checked random-access" (or "checked dictionary access"). So the actual requirements aren't very clear. In terms of "C++ at" equivalent, I don't think we have anything equivalent to offer. That said, I've never seen anyone use "at" in C++ ever. I'd assume it's more of a
Jul 27 2014
On Sun, Jul 27, 2014 at 07:42:17PM -0700, Timothee Cour via Digitalmars-d-learn wrote:Just for clarification, I wanted 'myrange.at(i)' to be the same as `myrange.dropExactly(i).front` (so I don't assume it's a random access range).[...] You could just define your own function for it, right? // or call it whatever you want auto getNth(R)(R range, size_t index) if (isInputRange!R) { return range.dropExactly(index).front; } T -- Making non-nullable pointers is just plugging one hole in a cheese grater. -- Walter Brightdoing and that it's inefficient. It might be necessary in some cases, but we don't want to give the impression that it's cheap, which at() would do. I think it's already clear that it's potentially O(n) [n=i] cost as we're not using myrange[i]. But fine, call it atWalk/walkAt/whatever. Point is it's a common enough operation.myrange.dropExactly(i).front makes it much more obvious what you're
Jul 27 2014
On Sun, Jul 27, 2014 at 9:20 PM, H. S. Teoh via Digitalmars-d-learn < digitalmars-d-learn puremagic.com> wrote:On Sun, Jul 27, 2014 at 07:42:17PM -0700, Timothee Cour via Digitalmars-d-learn wrote:Obviously I did that, but I thought it belonged in phobos. Anyway, closing this.Just for clarification, I wanted 'myrange.at(i)' to be the same as `myrange.dropExactly(i).front` (so I don't assume it's a random access range).[...] You could just define your own function for it, right? // or call it whatever you want auto getNth(R)(R range, size_t index) if (isInputRange!R) { return range.dropExactly(index).front; } T -- Making non-nullable pointers is just plugging one hole in a cheese grater. -- Walter Brightdoing and that it's inefficient. It might be necessary in some cases, but we don't want to give the impression that it's cheap, which at() would do. I think it's already clear that it's potentially O(n) [n=i] cost as we're not using myrange[i]. But fine, call it atWalk/walkAt/whatever. Point is it's a common enough operation.myrange.dropExactly(i).front makes it much more obvious what you're
Jul 29 2014