unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* vector-last / vector-ref with negative indices?
@ 2020-12-20  6:58 Aleix Conchillo Flaqué
  2020-12-20 10:05 ` Christopher Lam
  2020-12-21  9:50 ` Aleix Conchillo Flaqué
  0 siblings, 2 replies; 5+ messages in thread
From: Aleix Conchillo Flaqué @ 2020-12-20  6:58 UTC (permalink / raw)
  To: guile-user

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



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: vector-last / vector-ref with negative indices?
  2020-12-20  6:58 vector-last / vector-ref with negative indices? Aleix Conchillo Flaqué
@ 2020-12-20 10:05 ` Christopher Lam
  2020-12-20 10:41   ` lloda
  2020-12-21  9:50 ` Aleix Conchillo Flaqué
  1 sibling, 1 reply; 5+ messages in thread
From: Christopher Lam @ 2020-12-20 10:05 UTC (permalink / raw)
  To: Aleix Conchillo Flaqué; +Cc: guile-user

Easy - vector-ref with a negative index is not defined in r[5|7]rs.

On Sun, 20 Dec 2020 at 06:59, Aleix Conchillo Flaqué <aconchillo@gmail.com>
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
>
>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: vector-last / vector-ref with negative indices?
  2020-12-20 10:05 ` Christopher Lam
@ 2020-12-20 10:41   ` lloda
  0 siblings, 0 replies; 5+ messages in thread
From: lloda @ 2020-12-20 10:41 UTC (permalink / raw)
  To: Christopher Lam; +Cc: guile-user


Besides the standard which applies to vector- operations, negative indices wouldn't work for Guile arrays to count from the end since negative indices, like in say Fortran, can be valid [e.g. (make-array 0 '(-1 4))]. In older versions of Guile you could use vector-ref on such arrays.

I don't think allowing negative indices is worthwhile for all the complications it introduces. But I don't like negative indices to count from the end either. Most of the time vectors aren't meant to have that wraparound property so it can turn a obvious error into an obscure one.

The solution I like best is to have a keyword that means 'the end', like in Octave, that can be used generically. Barring that you can easily define vector-last or even something like vector-ref-from-end I guess. I think it's weird that srfi-43 doesn't have vector-last, when srfi-1 has last. Maybe Guile could add that as an extension, although I don't like srfi-43 in general (I think the map functions are wrongly designed).

regards

	Daniel


> On 20 Dec 2020, at 11:05, Christopher Lam <christopher.lck@gmail.com> wrote:
> 
> Easy - vector-ref with a negative index is not defined in r[5|7]rs.
> 
> On Sun, 20 Dec 2020 at 06:59, Aleix Conchillo Flaqué <aconchillo@gmail.com>
> 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
>> 
>> 




^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: vector-last / vector-ref with negative indices?
@ 2020-12-20 13:44 dsmich
  0 siblings, 0 replies; 5+ messages in thread
From: dsmich @ 2020-12-20 13:44 UTC (permalink / raw)
  To: 'Aleix Conchillo Flaqué'; +Cc: 'guile-user'


Maybe sort the vector in the other direction and access with index 0
instead?

-Dale

	-----------------------------------------From: "Aleix Conchillo
Flaqué" 
To: "guile-user"
Cc: 
Sent: Sunday December 20 2020 1:59:04AM
Subject: vector-last / vector-ref with negative indices?

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




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: vector-last / vector-ref with negative indices?
  2020-12-20  6:58 vector-last / vector-ref with negative indices? Aleix Conchillo Flaqué
  2020-12-20 10:05 ` Christopher Lam
@ 2020-12-21  9:50 ` Aleix Conchillo Flaqué
  1 sibling, 0 replies; 5+ messages in thread
From: Aleix Conchillo Flaqué @ 2020-12-21  9:50 UTC (permalink / raw)
  To: guile-user

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é
<aconchillo@gmail.com> 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



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-12-21  9:50 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-20  6:58 vector-last / vector-ref with negative indices? Aleix Conchillo Flaqué
2020-12-20 10:05 ` 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

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).