unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: "Aleix Conchillo Flaqué" <aconchillo@gmail.com>
To: guile-user <guile-user@gnu.org>
Subject: vector-last / vector-ref with negative indices?
Date: Sat, 19 Dec 2020 22:58:40 -0800	[thread overview]
Message-ID: <CA+XASoWHOs6+EfaD22+yu44-ObJEJkzsajC49A8Ce8RtjNkC4Q@mail.gmail.com> (raw)

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	other threads:[~2020-12-20  6:58 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-20  6:58 Aleix Conchillo Flaqué [this message]
2020-12-20 10:05 ` vector-last / vector-ref with negative indices? Christopher Lam
2020-12-20 10:41   ` lloda
2020-12-21  9:50 ` Aleix Conchillo Flaqué
  -- strict thread matches above, loose matches on Subject: below --
2020-12-20 13:44 dsmich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CA+XASoWHOs6+EfaD22+yu44-ObJEJkzsajC49A8Ce8RtjNkC4Q@mail.gmail.com \
    --to=aconchillo@gmail.com \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).