From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: =?UTF-8?Q?Aleix_Conchillo_Flaqu=C3=A9?= Newsgroups: gmane.lisp.guile.user Subject: Re: vector-last / vector-ref with negative indices? Date: Mon, 21 Dec 2020 01:50:08 -0800 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="9079"; mail-complaints-to="usenet@ciao.gmane.io" To: guile-user Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Mon Dec 21 10:50:39 2020 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1krHpu-0002Be-Hk for guile-user@m.gmane-mx.org; Mon, 21 Dec 2020 10:50:38 +0100 Original-Received: from localhost ([::1]:59276 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1krHpt-0002Vo-De for guile-user@m.gmane-mx.org; Mon, 21 Dec 2020 04:50:37 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:35860) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1krHph-0002Uv-H6 for guile-user@gnu.org; Mon, 21 Dec 2020 04:50:25 -0500 Original-Received: from mail-pj1-x1031.google.com ([2607:f8b0:4864:20::1031]:39935) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1krHpd-0004Te-8W for guile-user@gnu.org; Mon, 21 Dec 2020 04:50:25 -0500 Original-Received: by mail-pj1-x1031.google.com with SMTP id hk16so6151226pjb.4 for ; Mon, 21 Dec 2020 01:50:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :content-transfer-encoding; bh=vTJGCkiOdb6pTtV8aeQyRXIO6y+nDA7pFV56STjlcy8=; b=jfxJPW6mEDt+dlV2vFjgmXFkFUlrnAgLBOjDpbYcsJICTNfB1YkpZDNG0cRCg+fbQ7 QeLnG1mA1Q2isWCgG3AwCxT275CsHZAVic0kAYzSrFvyxOZOORaNWVd5oYzjaS2uEc+/ sSuFL/sJNkb/xhR+kEDQcMogPT83R4S0p62B2RDQR6jb7yQRLo8sfPG/qmj3ZHU+zp81 XgPPeeczoMfAAD1NULW2TmeLz4hgIGSCw7kVl9fKoU6HJcc/jmMn8XNseOWdbbFjZzyz 4grJkKJDmMcnBNarNio9GkDzNkUlhXtecfkIIDGzvRIR8ZrHgf9/bTeT6yWGQ24JYjW9 YMIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:content-transfer-encoding; bh=vTJGCkiOdb6pTtV8aeQyRXIO6y+nDA7pFV56STjlcy8=; b=JcALd3Etv6YYxr1bWc/HrB/vpyL2qe3FTLRpiz2I5UscgwlexW+YcZL/OTsBWPyQKt lGztQOLqnDJNgsqCV/H8az3aNDoJ0kKloNu93bANKTawiYneODVCwIo+v+BUl8m62Ota d0uQxcsbZQRKpDaHEDD0Sx+oW9MJTVr635ruSuBluFejMyjmkECHpDCjmarPsC9Vjjx8 JyY/pC6nVC1Obc9qQ26TMh7rA6NrFfHZQhPMCwukuCAQQD1jDlSwJbQrIsZ7tNXzo1Hw Pu8cnNU2IjuYxIU7NIZ5vbhBTuvqW8ZOsIfEJPDIApVxe/ie+4vFILWLQVKlTtKRfZYP sgWA== X-Gm-Message-State: AOAM531KCmtR/f7fl21KxeDEgVOTn3OhdLp95DonlRydc7miTKoajgG8 05w+xtV5z/K9OwIWpF1kGzYSGKyYdzOmT3l0FSeNiUkPcmiMCA== X-Google-Smtp-Source: ABdhPJxMmcqrj9NXlwYathEDm3I7Wneg3U2aoC7LPR4c5uC/A4cbCAW9BBtH1Z3VfK9Xvo1HMWj/5UAYme2uo0tAUc4= X-Received: by 2002:a17:90b:90f:: with SMTP id bo15mr16216156pjb.148.1608544219135; Mon, 21 Dec 2020 01:50:19 -0800 (PST) In-Reply-To: Received-SPF: pass client-ip=2607:f8b0:4864:20::1031; envelope-from=aconchillo@gmail.com; helo=mail-pj1-x1031.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:17098 Archived-At: Thank you for the replies. I ended up sending a patch to add support for (vector-last) since I found it useful. https://lists.gnu.org/archive/html/guile-devel/2020-12/msg00009.html Aleix On Sat, Dec 19, 2020 at 10:58 PM Aleix Conchillo Flaqu=C3=A9 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