[Top][All Lists]

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

Re: [PATCH] seq.el: add seq-last for symmetry with seq-first

From: Philip Kaludercic
Subject: Re: [PATCH] seq.el: add seq-last for symmetry with seq-first
Date: Wed, 15 Mar 2023 08:09:40 +0000

Augusto Stoffel <arstoffel@gmail.com> writes:

> On Tue, 14 Mar 2023 at 22:04, Philip Kaludercic wrote:
>>>> Maybe it's more useful to allow negative arguments in seq-elt?  Saying
>>>> (seq-elt seq -1) isn't much more effort than (seq-last seq).
>>> I'm personally a bit ambiguous about negative indices.  It's nice for
>>> those one liners when you need them, but they are quite confusing
>>> especially if you switch between languages and they all implement them a
>>> bit differently.
>> The only language I am really familiar with is python, and what that
>> effectively does is (mod i (length n)), what do other languages do?
> In Python "abc"[-4] throws and error.  I don't think there's any choice
> to be made here: If it were to support a negative index n, then
> (seq-elt s n) should just return the (+ (length s) n)-th element, with
> the usual treatment for out of bounds indices (whatever it is).
>> Another thing that should be kept in mind that sequences can be streams
>> (as provided by stream.el), and there doesn't have to be a final
>> element.
> What is the seq-length of an infinite stream?  And does asking for the
> length of a stream consume it?  If so, then seq is an imperfect
> abstraction for streams.

Yes it does, since `seq' requires `seq-length' to be implemented for
every new sequence:

--8<---------------cut here---------------start------------->8---
(cl-defmethod seq-length ((stream stream))
  "Return the length of STREAM.
This function will eagerly consume the entire stream."
  (let ((len 0))
    (while (not (stream-empty-p stream))
      (setq len (1+ len))
      (setq stream (stream-rest stream)))
--8<---------------cut here---------------end--------------->8---

The only alternative I see here would be that streams raise a signal, if
you try to determine their length (or rather only if they are infinite,
which I don't know if you can determine without eager evaluation).

reply via email to

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