unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How to specialize seq-concatenate and seq-into-sequence?
@ 2022-03-03 15:50 Philipp Stephani
  2022-03-03 17:40 ` Stefan Monnier via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 12+ messages in thread
From: Philipp Stephani @ 2022-03-03 15:50 UTC (permalink / raw)
  To: Help Gnu Emacs mailing list

Hi,

I'm trying to specialize the seq.el functions for a new sequence type.  This is mostly straight-forward, except for two functions: seq-concatenate and seq-into-sequence.

1. Is the idea that seq-into-sequence should return a "primitive" sequence (vector, list, string)?  At least that's my interpretation, but the docstring isn't 100% clear.

2. seq-concatenate takes a &rest argument - AIUI it's not possible to specialize that?

Thanks,
Philipp


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

* Re: How to specialize seq-concatenate and seq-into-sequence?
  2022-03-03 15:50 How to specialize seq-concatenate and seq-into-sequence? Philipp Stephani
@ 2022-03-03 17:40 ` Stefan Monnier via Users list for the GNU Emacs text editor
  2022-03-03 20:49   ` Philipp Stephani
  0 siblings, 1 reply; 12+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2022-03-03 17:40 UTC (permalink / raw)
  To: help-gnu-emacs

Philipp Stephani [2022-03-03 16:50:00] wrote:
> 1. Is the idea that seq-into-sequence should return a "primitive" sequence
> (vector, list, string)?  At least that's my interpretation, but the
> docstring isn't 100% clear.

I think that's the idea, yes.

> 2. seq-concatenate takes a &rest argument - AIUI it's not possible to specialize that?

I think the idea is that you'd use something like `seq-do` on each
sequence.  AFAICT `stream.el` doesn't implement this method, tho.


        Stefan




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

* Re: How to specialize seq-concatenate and seq-into-sequence?
  2022-03-03 17:40 ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2022-03-03 20:49   ` Philipp Stephani
  2022-03-04  0:20     ` Stefan Monnier
  0 siblings, 1 reply; 12+ messages in thread
From: Philipp Stephani @ 2022-03-03 20:49 UTC (permalink / raw)
  To: Stefan Monnier,
	Stefan Monnier via Users list for the GNU Emacs text editor



> Am 03.03.2022 um 18:40 schrieb Stefan Monnier via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org>:
> 
> Philipp Stephani [2022-03-03 16:50:00] wrote:
>> 1. Is the idea that seq-into-sequence should return a "primitive" sequence
>> (vector, list, string)?  At least that's my interpretation, but the
>> docstring isn't 100% clear.
> 
> I think that's the idea, yes.

OK, thanks for confirming.

> 
>> 2. seq-concatenate takes a &rest argument - AIUI it's not possible to specialize that?
> 
> I think the idea is that you'd use something like `seq-do` on each
> sequence.

But how would one go about writing the cl-defmethod for it?




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

* Re: How to specialize seq-concatenate and seq-into-sequence?
  2022-03-03 20:49   ` Philipp Stephani
@ 2022-03-04  0:20     ` Stefan Monnier
  2022-03-04  0:48       ` Philipp Stephani
  0 siblings, 1 reply; 12+ messages in thread
From: Stefan Monnier @ 2022-03-04  0:20 UTC (permalink / raw)
  To: Philipp Stephani
  Cc: Stefan Monnier via Users list for the GNU Emacs text editor

>>> 2. seq-concatenate takes a &rest argument - AIUI it's not possible to specialize that?
>> I think the idea is that you'd use something like `seq-do` on each
>> sequence.
> But how would one go about writing the cl-defmethod for it?

Define `it`.
The dispatch is expected to be on the `type` argument (using an `eql` specializer).



        Stefan




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

* Re: How to specialize seq-concatenate and seq-into-sequence?
  2022-03-04  0:20     ` Stefan Monnier
@ 2022-03-04  0:48       ` Philipp Stephani
  2022-03-04  2:35         ` Michael Heerdegen
  2022-03-04 13:31         ` Stefan Monnier
  0 siblings, 2 replies; 12+ messages in thread
From: Philipp Stephani @ 2022-03-04  0:48 UTC (permalink / raw)
  To: Stefan Monnier
  Cc: Stefan Monnier via Users list for the GNU Emacs text editor



> Am 04.03.2022 um 01:20 schrieb Stefan Monnier <monnier@iro.umontreal.ca>:
> 
>>>> 2. seq-concatenate takes a &rest argument - AIUI it's not possible to specialize that?
>>> I think the idea is that you'd use something like `seq-do` on each
>>> sequence.
>> But how would one go about writing the cl-defmethod for it?
> 
> Define `it`.
> The dispatch is expected to be on the `type` argument (using an `eql` specializer).
> 

Doesn't that contradict the docstring?

   TYPE must be one of following symbols: vector, string or list.


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

* Re: How to specialize seq-concatenate and seq-into-sequence?
  2022-03-04  0:48       ` Philipp Stephani
@ 2022-03-04  2:35         ` Michael Heerdegen
  2022-03-04 14:31           ` Stefan Monnier via Users list for the GNU Emacs text editor
  2022-03-04 20:56           ` Philipp Stephani
  2022-03-04 13:31         ` Stefan Monnier
  1 sibling, 2 replies; 12+ messages in thread
From: Michael Heerdegen @ 2022-03-04  2:35 UTC (permalink / raw)
  To: help-gnu-emacs

Philipp Stephani <p.stephani2@gmail.com> writes:

> > Define `it`.
> > The dispatch is expected to be on the `type` argument (using an
> > `eql` specializer).
> > 
>
> Doesn't that contradict the docstring?

I'm also a bit confused.  How would you concatenate a list and a stream
to a list?  Or how would you implement it?

Would wrapping `seq-into' over the SEQUENCES be better than only relying
on dispatching?

Michael.




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

* Re: How to specialize seq-concatenate and seq-into-sequence?
  2022-03-04  0:48       ` Philipp Stephani
  2022-03-04  2:35         ` Michael Heerdegen
@ 2022-03-04 13:31         ` Stefan Monnier
  1 sibling, 0 replies; 12+ messages in thread
From: Stefan Monnier @ 2022-03-04 13:31 UTC (permalink / raw)
  To: Philipp Stephani
  Cc: Stefan Monnier via Users list for the GNU Emacs text editor

Philipp Stephani [2022-03-04 01:48:56] wrote:
>> Define `it`.
>> The dispatch is expected to be on the `type` argument (using an `eql` specializer).
> Doesn't that contradict the docstring?
>
>    TYPE must be one of following symbols: vector, string or list.

That's a docstring bug, I think.


        Stefan




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

* Re: How to specialize seq-concatenate and seq-into-sequence?
  2022-03-04  2:35         ` Michael Heerdegen
@ 2022-03-04 14:31           ` Stefan Monnier via Users list for the GNU Emacs text editor
  2022-03-04 22:12             ` Michael Heerdegen
  2022-03-04 20:56           ` Philipp Stephani
  1 sibling, 1 reply; 12+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2022-03-04 14:31 UTC (permalink / raw)
  To: help-gnu-emacs

> I'm also a bit confused.  How would you concatenate a list and a stream
> to a list?

I don't understand the question.

> Or how would you implement it?

`seq-do` iteration on the various sequences seems like an
obvious solution.

> Would wrapping `seq-into' over the SEQUENCES be better than only relying
> on dispatching?

I don't understand this question either.


        Stefan




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

* Re: How to specialize seq-concatenate and seq-into-sequence?
  2022-03-04  2:35         ` Michael Heerdegen
  2022-03-04 14:31           ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2022-03-04 20:56           ` Philipp Stephani
  2022-03-04 21:54             ` Michael Heerdegen
  1 sibling, 1 reply; 12+ messages in thread
From: Philipp Stephani @ 2022-03-04 20:56 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: help-gnu-emacs



> Am 04.03.2022 um 03:35 schrieb Michael Heerdegen <michael_heerdegen@web.de>:
> 
> Philipp Stephani <p.stephani2@gmail.com> writes:
> 
>>> Define `it`.
>>> The dispatch is expected to be on the `type` argument (using an
>>> `eql` specializer).
>>> 
>> 
>> Doesn't that contradict the docstring?
> 
> I'm also a bit confused.  How would you concatenate a list and a stream
> to a list?  Or how would you implement it?

I guess the idea is that `seq-concatenate' can only be specialized for new *result* types, not for new *argument* types.  That is, if you have a new sequence-like type `my-sequence', you can specialize it like so:

(cl-defmethod ((_type (eql my-sequence)) &rest sequences)
  (my-sequence-concatenate (mapcar #'convert-to-my-sequence sequences)))

where `my-sequence-concatenate' and `convert-to-my-sequence' are functions specific to the new sequence type.  It's not possible to convert a `my-sequence' to a list this way; for that you'd need to specialize `seq-into' or `seq-into-sequence' instead.


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

* Re: How to specialize seq-concatenate and seq-into-sequence?
  2022-03-04 20:56           ` Philipp Stephani
@ 2022-03-04 21:54             ` Michael Heerdegen
  0 siblings, 0 replies; 12+ messages in thread
From: Michael Heerdegen @ 2022-03-04 21:54 UTC (permalink / raw)
  To: Philipp Stephani; +Cc: help-gnu-emacs

Philipp Stephani <p.stephani2@gmail.com> writes:

> I guess the idea is that `seq-concatenate' can only be specialized for
> new *result* types, not for new *argument* types.

Likely.  But that won't be enough when more sequence types are added.

I think each argument that is not one of the default types should be
silently converted into the goal type before appending the sequences.
The generic function `seq-into' seems the right place to implement the
`seq-concatenate' semantics for new sequence types.

Michael.



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

* Re: How to specialize seq-concatenate and seq-into-sequence?
  2022-03-04 14:31           ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2022-03-04 22:12             ` Michael Heerdegen
  2022-03-04 23:02               ` Stefan Monnier via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 12+ messages in thread
From: Michael Heerdegen @ 2022-03-04 22:12 UTC (permalink / raw)
  To: help-gnu-emacs

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

Stefan Monnier via Users list for the GNU Emacs text editor
<help-gnu-emacs@gnu.org> writes:

> > How would you concatenate a list and a stream to a list?
>
> I don't understand the question.

I wanted to imply that (seq-into 'list (list 1 2) (stream (list 3 4)))
errors.

> > Or how would you implement it?
>
> `seq-do` iteration on the various sequences seems like an
> obvious solution.
>
> > Would wrapping `seq-into' over the SEQUENCES be better than only relying
> > on dispatching?
>
> I don't understand this question either.

Why `seq-do'?  My idea was to do this:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-WIP-seq-concatenate-transform-SEQUENCE-arguments.patch --]
[-- Type: text/x-diff, Size: 760 bytes --]

From 9937c190af8a9cb95c52039ef98e26ac9a2e9fe0 Mon Sep 17 00:00:00 2001
From: Michael Heerdegen <michael_heerdegen@web.de>
Date: Fri, 4 Mar 2022 23:09:28 +0100
Subject: [PATCH] WIP: seq-concatenate: transform SEQUENCE arguments

---
 lisp/emacs-lisp/seq.el | 1 +
 1 file changed, 1 insertion(+)

diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el
index cb1b822f85..331e5755d8 100644
--- a/lisp/emacs-lisp/seq.el
+++ b/lisp/emacs-lisp/seq.el
@@ -299,6 +299,7 @@ seq-concatenate
 TYPE must be one of following symbols: vector, string or list.
 
 \n(fn TYPE SEQUENCE...)"
+  (setq sequences (mapcar (lambda (s) (seq-into s type)) sequences))
   (pcase type
     ('vector (apply #'vconcat sequences))
     ('string (apply #'concat sequences))
-- 
2.30.2


[-- Attachment #3: Type: text/plain, Size: 10 bytes --]



Michael

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

* Re: How to specialize seq-concatenate and seq-into-sequence?
  2022-03-04 22:12             ` Michael Heerdegen
@ 2022-03-04 23:02               ` Stefan Monnier via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 12+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2022-03-04 23:02 UTC (permalink / raw)
  To: help-gnu-emacs

> Why `seq-do'?

To avoid allocating intermediate sequences.
But yes, there are other solutions, with different tradeoffs.


        Stefan




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

end of thread, other threads:[~2022-03-04 23:02 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-03-03 15:50 How to specialize seq-concatenate and seq-into-sequence? Philipp Stephani
2022-03-03 17:40 ` Stefan Monnier via Users list for the GNU Emacs text editor
2022-03-03 20:49   ` Philipp Stephani
2022-03-04  0:20     ` Stefan Monnier
2022-03-04  0:48       ` Philipp Stephani
2022-03-04  2:35         ` Michael Heerdegen
2022-03-04 14:31           ` Stefan Monnier via Users list for the GNU Emacs text editor
2022-03-04 22:12             ` Michael Heerdegen
2022-03-04 23:02               ` Stefan Monnier via Users list for the GNU Emacs text editor
2022-03-04 20:56           ` Philipp Stephani
2022-03-04 21:54             ` Michael Heerdegen
2022-03-04 13:31         ` Stefan Monnier

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