[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: vector-last / vector-ref with negative indices?

From: Aleix Conchillo Flaqué
Subject: Re: vector-last / vector-ref with negative indices?
Date: Mon, 21 Dec 2020 01:50:08 -0800

Thank you for the replies. I ended up sending a patch to add support
for (vector-last) since I found it useful.


On Sat, Dec 19, 2020 at 10:58 PM Aleix Conchillo Flaqué
<> wrote:
> Hi,
> This month I'm trying to go through Advent Of Code one more year
> (previous years I didn't get too far) and I've been finding myself
> writing the same patterns multiple times that could be avoided by just
> having a helper function.
> One of them is getting the last element of a vector. It is a quite
> common operation when solving these types of problems. For example
> Python as you might know uses negative indices.
> I have looked around and haven't seen it (unless I completely missed
> it which would be shameful) but wouldn't it be good to have this
> built-in by default?
> Instead of having to write:
> (vector-ref v (- (vector-length v) 1))
> you would write:
> (vector-last v) or even better (vector-ref v -1).
> Interestingly Racket doesn't offer those functions either as far as I can 
> tell.
> Basic use case? Get the maximum element of a sorted vector which would
> be done in constant time.
> I'm trying to solve the problems idiomatically, to the best of my
> limited knowledge, with Scheme. It is possible that the times I'm
> using vectors I should think about it differently and not use them,
> but I'm not 100% sure.
> What am I missing?
> Thank you in advance,
> Aleix

reply via email to

[Prev in Thread] Current Thread [Next in Thread]