unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Philip Kaludercic <philipk@posteo.net>
To: Augusto Stoffel <arstoffel@gmail.com>
Cc: "Matúš Goljer" <matus.goljer@gmail.com>, emacs-devel@gnu.org
Subject: Re: [PATCH] seq.el: add seq-last for symmetry with seq-first
Date: Wed, 15 Mar 2023 08:09:40 +0000	[thread overview]
Message-ID: <87lejybfln.fsf@posteo.net> (raw)
In-Reply-To: <87mt4ebij7.fsf@gmail.com> (Augusto Stoffel's message of "Wed, 15 Mar 2023 08:06:20 +0100")

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)))
    len))
--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	other threads:[~2023-03-15  8:09 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-14 12:55 [PATCH] seq.el: add seq-last for symmetry with seq-first Matúš Goljer
2023-03-14 15:26 ` Philip Kaludercic
2023-03-14 15:48   ` Matúš Goljer
2023-03-14 16:14     ` Philip Kaludercic
2023-03-14 18:35       ` Augusto Stoffel
2023-03-14 19:14         ` Matúš Goljer
2023-03-14 22:04         ` Philip Kaludercic
2023-03-15  7:06           ` Augusto Stoffel
2023-03-15  8:09             ` Philip Kaludercic [this message]

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/emacs/

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

  git send-email \
    --in-reply-to=87lejybfln.fsf@posteo.net \
    --to=philipk@posteo.net \
    --cc=arstoffel@gmail.com \
    --cc=emacs-devel@gnu.org \
    --cc=matus.goljer@gmail.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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