Thread overview
myrange.at(i) for myrange.dropExactly(i).front
Jul 25, 2014
Timothee Cour
Jul 25, 2014
Jonathan M Davis
Jul 26, 2014
Ary Borenszweig
Jul 26, 2014
Jonathan M Davis
Jul 26, 2014
monarch_dodra
Jul 28, 2014
Timothee Cour
Jul 28, 2014
H. S. Teoh
Jul 30, 2014
Timothee Cour
July 25, 2014
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)


July 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
July 26, 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:
>> 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.
July 26, 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:
>> 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
July 26, 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. I'd assume it's more of a java/C# thing to do checked accesses?

July 28, 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).

>> 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?
>
>


July 28, 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).
> 
> >> 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
July 30, 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:
> > 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.