all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Defining new sequence types for seq.el
@ 2019-11-12  0:43 Eric Abrahamsen
  2019-11-12  3:02 ` Juanma Barranquero
  0 siblings, 1 reply; 4+ messages in thread
From: Eric Abrahamsen @ 2019-11-12  0:43 UTC (permalink / raw)
  To: emacs-devel

My apologies if I'm missing something very obvious here, but seq.el and
the accompanying documentation mention defining new sequence types, and
I don't actually see how you go about doing that. The library uses
generic functions, so you'd need something that can be specialized on,
but... how?

Say for the sake of argument that I'd like to define a new sequence type
called `gnus-range', which is nothing more than a list of integers, but
where a list like (1 4 5 6 7 12 21 22 23) would be represented as
(1 (4 . 7) 12 (21 . 23)), in order to save space (yes I know that example
doesn't save much space, these are very long lists).

This seems like a great use for the generic functions in seq: for
instance, (seq-elt '((1 . 9) 35 120) 0) could return 1 instead of (1 . 9).

But I don't know how to make the type identifiable to the generic
functions. I tried:

(cl-deftype gnus-range () '(or null cons))

But generic methods don't check deftypes, and also that's the same
definition as for lists, which seems like it could wreak havoc.

Do I need to use a struct with one slot? Any other tips?

Thanks,
Eric




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

* Re: Defining new sequence types for seq.el
  2019-11-12  0:43 Defining new sequence types for seq.el Eric Abrahamsen
@ 2019-11-12  3:02 ` Juanma Barranquero
  2019-11-13  0:16   ` Eric Abrahamsen
  0 siblings, 1 reply; 4+ messages in thread
From: Juanma Barranquero @ 2019-11-12  3:02 UTC (permalink / raw)
  To: Eric Abrahamsen; +Cc: Emacs developers

[-- Attachment #1: Type: text/plain, Size: 419 bytes --]

On Tue, Nov 12, 2019 at 1:45 AM Eric Abrahamsen <eric@ericabrahamsen.net>
wrote:

> Do I need to use a struct with one slot? Any other tips?

You could do something like:

(setq my-list '(gnus-range (1 . 9) 35 120))

(cl-defgeneric seq-length ((sequence (head gnus-range)))
  (let ((l 0))
    (dolist (elt (cdr sequence))
      (cl-incf l (if (consp elt) (1+ (- (cdr elt) (car elt))) 1)))
    l))

(seq-length my-list)

[-- Attachment #2: Type: text/html, Size: 616 bytes --]

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

* Re: Defining new sequence types for seq.el
  2019-11-12  3:02 ` Juanma Barranquero
@ 2019-11-13  0:16   ` Eric Abrahamsen
  2019-11-13  1:04     ` Noam Postavsky
  0 siblings, 1 reply; 4+ messages in thread
From: Eric Abrahamsen @ 2019-11-13  0:16 UTC (permalink / raw)
  To: emacs-devel

Juanma Barranquero <lekktu@gmail.com> writes:

> On Tue, Nov 12, 2019 at 1:45 AM Eric Abrahamsen <eric@ericabrahamsen.net>
> wrote:
>
>> Do I need to use a struct with one slot? Any other tips?
>
> You could do something like:
>
> (setq my-list '(gnus-range (1 . 9) 35 120))

That did occur to me at some point, but seemed sort of hacky. But yes,
it would work!

I guess now I'm wondering how much benefit there is to integrating this
with seq. How much code would be able to profit from the polymorphism,
where we wanted to not to care whether a value was a range or a list? On
the other hand, maybe it would be nice just to be able to hide the
implementation.

What I really wish is that I knew C, and could implement these as
integer sets, at the C level. Maybe in a future career...

Thanks for your answer, and for letting me talk out loud.

Eric





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

* Re: Defining new sequence types for seq.el
  2019-11-13  0:16   ` Eric Abrahamsen
@ 2019-11-13  1:04     ` Noam Postavsky
  0 siblings, 0 replies; 4+ messages in thread
From: Noam Postavsky @ 2019-11-13  1:04 UTC (permalink / raw)
  To: Eric Abrahamsen; +Cc: Emacs developers

On Tue, 12 Nov 2019 at 19:17, Eric Abrahamsen <eric@ericabrahamsen.net> wrote:

> >> Do I need to use a struct with one slot? Any other tips?
> >
> > You could do something like:
> >
> > (setq my-list '(gnus-range (1 . 9) 35 120))
>
> That did occur to me at some point, but seemed sort of hacky. But yes,
> it would work!

I think your original idea of a struct with one slot is a bit cleaner.



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

end of thread, other threads:[~2019-11-13  1:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-11-12  0:43 Defining new sequence types for seq.el Eric Abrahamsen
2019-11-12  3:02 ` Juanma Barranquero
2019-11-13  0:16   ` Eric Abrahamsen
2019-11-13  1:04     ` Noam Postavsky

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.