From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: dsmich@roadrunner.com Newsgroups: gmane.lisp.guile.user Subject: RE: vector-last / vector-ref with negative indices? Date: Sun, 20 Dec 2020 13:44:18 +0000 Message-ID: <0fb185d6aaa0fb76fdb4df1cc619d138f056fcb7@webmail> 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="15106"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 'guile-user' To: "'=?UTF-8?B?QWxlaXggQ29uY2hpbGxvIEZsYXF1w6k=?='" Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sun Dec 20 14:44:47 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 1kqz0x-0003pC-PX for guile-user@m.gmane-mx.org; Sun, 20 Dec 2020 14:44:47 +0100 Original-Received: from localhost ([::1]:55356 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kqz0w-0006jz-Ky for guile-user@m.gmane-mx.org; Sun, 20 Dec 2020 08:44:46 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:43248) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kqz0Z-0006jj-F0 for guile-user@gnu.org; Sun, 20 Dec 2020 08:44:23 -0500 Original-Received: from p-impout003aa.msg.pkvw.co.charter.net ([47.43.26.134]:60833 helo=p-impout003.msg.pkvw.co.charter.net) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kqz0X-00058U-Q7 for guile-user@gnu.org; Sun, 20 Dec 2020 08:44:23 -0500 Original-Received: from localhost ([54.243.7.172]) by cmsmtp with ESMTP id qz0UkbBDSItLOqz0UkwGWz; Sun, 20 Dec 2020 13:44:18 +0000 X-Authority-Analysis: v=2.3 cv=QPMWuTDL c=1 sm=1 tr=0 a=ZhHbwgLlxfPm5G22ibXkng==:117 a=ZhHbwgLlxfPm5G22ibXkng==:17 a=dSg6v7tjMxUA:10 a=DGoUTgDXHqdBIMB5dgQA:9 a=QEXdDO2ut3YA:10 a=pGLkceISAAAA:8 a=JmiodjPpQJ0Iy5QdMWkA:9 a=vChwAuwlcwdDmQ1e:21 a=_W_S_7VecoQA:10 X-Mailer: Atmail X-Originating-IP: [65.185.85.154] X-Priority: 3 Importance: Normal X-MSMail-Priority: Normal X-CMAE-Envelope: MS4wfGx1gap73u1zQB6K5moOIxjNGBIN6k7btD3cX41re6t0GrQg6QVBwshT8xxugLl+HoH8jRyTGanZmF79DoC7YVvTQGxJuFwTtXgKcoT+e1mDaDJ2Xlrq NxcBS8Bs/oLRQnWMP94k1ytYv+pE0ce6440LINHumsLmmAXf+txq7eg1oom5P0czUYVXOJWBr3ahQA== Received-SPF: pass client-ip=47.43.26.134; envelope-from=dsmich@roadrunner.com; helo=p-impout003.msg.pkvw.co.charter.net X-Spam_score_int: 0 X-Spam_score: 0.0 X-Spam_bar: / X-Spam_report: (0.0 / 5.0 requ) BAYES_00=-1.9, HTML_MESSAGE=0.001, MISSING_MIMEOLE=1.899, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-Content-Filtered-By: Mailman/MimeDel 2.1.23 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:17093 Archived-At: =0AMaybe sort the vector in the other direction and access with index 0= =0Ainstead?=0A=0A-Dale=0A=0A=09-----------------------------------------= From: "Aleix Conchillo=0AFlaqu=C3=A9" =0ATo: "guile-user"=0ACc: =0ASent:= Sunday December 20 2020 1:59:04AM=0ASubject: vector-last / vector-ref w= ith negative indices?=0A=0AHi,=0A=0A This month I'm trying to go through= Advent Of Code one more year=0A (previous years I didn't get too far) a= nd I've been finding myself=0A writing the same patterns multiple times= that could be avoided by=0A just=0A having a helper function.=0A=0A One= of them is getting the last element of a vector. It is a quite=0A commo= n operation when solving these types of problems. For example=0A Python= as you might know uses negative indices.=0A=0A I have looked around and= haven't seen it (unless I completely missed=0A it which would be shamef= ul) but wouldn't it be good to have this=0A built-in by default?=0A=0A I= nstead of having to write:=0A=0A (vector-ref v (- (vector-length v) 1))= =0A=0A you would write:=0A=0A (vector-last v) or even better (vector-ref= v -1).=0A=0A Interestingly Racket doesn't offer those functions either= as far as I=0A can tell.=0A=0A Basic use case? Get the maximum element= of a sorted vector which=0A would=0A be done in constant time.=0A=0A I'= m trying to solve the problems idiomatically, to the best of my=0A limit= ed knowledge, with Scheme. It is possible that the times I'm=0A using ve= ctors I should think about it differently and not use them,=0A but I'm n= ot 100% sure.=0A=0A What am I missing?=0A=0A Thank you in advance,=0A=0A= Aleix=0A=0A