www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - myrange.at(i) for myrange.dropExactly(i).front

reply Timothee Cour via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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
parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
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
parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
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:
 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
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.
Jul 25 2014
next sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Saturday, 26 July 2014 at 00:28:32 UTC, Ary Borenszweig wrote:
 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:
 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
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.
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 Davis
Jul 25 2014
prev sibling parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
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. I'd assume it's more of a java/C# thing to do checked accesses?
Jul 26 2014
next sibling parent Timothee Cour via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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).

 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. 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:
 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. I'd assume it's more of a java/C# thing to do checked accesses?
Jul 27 2014
prev sibling next sibling parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
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).
 
 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. 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.
[...] 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 Bright
Jul 27 2014
prev sibling parent Timothee Cour via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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:
 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).

 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. 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.
[...] 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 Bright
Obviously I did that, but I thought it belonged in phobos. Anyway, closing this.
Jul 29 2014