* Generic sequences in seq.el
@ 2018-12-21 11:42 Yuan Fu
2018-12-21 14:24 ` Stefan Monnier
0 siblings, 1 reply; 6+ messages in thread
From: Yuan Fu @ 2018-12-21 11:42 UTC (permalink / raw)
To: emacs-devel
[-- Attachment #1: Type: text/plain, Size: 784 bytes --]
I'm trying to "upgrade" some methods from supporting list to supporting
generic sequence.
However, I couldn't figure out how to change the value of a sequence
destructively.
E.g., if I want to insert a element into a list, I can do something like
(setf (nthcdr n seq) (cons element (nthcdr n seq)))
to insert element into the nth position in seq
But with sequences, I don't see any generic functions that can do similar
work.
Am I missing something?
BTW, what I'm doing now is define my own generic function, like
(cl-defgeneric my-insert-at (elt seq)))
The idea is I'll implement this function for the generic sequence
that I'll define in the future. Is that a correct approach?
Other destructive functions that I couldn't find alternatives are delete,
push, pop, etc.
[-- Attachment #2: Type: text/html, Size: 2165 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Generic sequences in seq.el
2018-12-21 11:42 Generic sequences in seq.el Yuan Fu
@ 2018-12-21 14:24 ` Stefan Monnier
2018-12-23 2:04 ` Yuan Fu
0 siblings, 1 reply; 6+ messages in thread
From: Stefan Monnier @ 2018-12-21 14:24 UTC (permalink / raw)
To: emacs-devel
> (setf (nthcdr n seq) (cons element (nthcdr n seq)))
> to insert element into the nth position in seq
I think with `seq` you'd do something like
(seq-concatenate 'list (seq-subseq seq 0 n)
(list element)
(seq-subseq seq n))
Of course, it's different because it's non-destructive, but insertion
into an array can't be done non-destructively anyway.
> The idea is I'll implement this function for the generic sequence
> that I'll define in the future. Is that a correct approach?
> Other destructive functions that I couldn't find alternatives are delete,
> push, pop, etc.
We currently don't have any data-structure that can be efficiently
modified in-place via push/pop (note that even lists don't qualify
because push/pop doesn't modify the list in place: they replace a list
with another without modifying any existing list (unless the PLACE is
the car/cdr of a list, of course, but that's a separate issue)).
Stefan
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Generic sequences in seq.el
2018-12-21 14:24 ` Stefan Monnier
@ 2018-12-23 2:04 ` Yuan Fu
2018-12-23 14:34 ` Stefan Monnier
0 siblings, 1 reply; 6+ messages in thread
From: Yuan Fu @ 2018-12-23 2:04 UTC (permalink / raw)
To: Stefan Monnier; +Cc: emacs-devel
[-- Attachment #1: Type: text/plain, Size: 1763 bytes --]
> (seq-concatenate 'list (seq-subseq seq 0 n)
> (list element)
> (seq-subseq seq n))
That returns a list, not the sequence. So inserting something into the
sequence
makes it not a sequence anymore?
> We currently don't have any data-structure that can be efficiently
> modified in-place via push/pop (note that even lists don't qualify
> because push/pop doesn't modify the list in place: they replace a list
> with another without modifying any existing list (unless the PLACE is
> the car/cdr of a list, of course, but that's a separate issue)).
Thanks, it's good to know.
Yuan.
On Fri, Dec 21, 2018 at 9:24 AM Stefan Monnier <monnier@iro.umontreal.ca>
wrote:
> > (setf (nthcdr n seq) (cons element (nthcdr n seq)))
> > to insert element into the nth position in seq
>
> I think with `seq` you'd do something like
>
> (seq-concatenate 'list (seq-subseq seq 0 n)
> (list element)
> (seq-subseq seq n))
>
> Of course, it's different because it's non-destructive, but insertion
> into an array can't be done non-destructively anyway.
>
> > The idea is I'll implement this function for the generic sequence
> > that I'll define in the future. Is that a correct approach?
> > Other destructive functions that I couldn't find alternatives are delete,
> > push, pop, etc.
>
> We currently don't have any data-structure that can be efficiently
> modified in-place via push/pop (note that even lists don't qualify
> because push/pop doesn't modify the list in place: they replace a list
> with another without modifying any existing list (unless the PLACE is
> the car/cdr of a list, of course, but that's a separate issue)).
>
>
> Stefan
>
>
>
[-- Attachment #2: Type: text/html, Size: 4637 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Generic sequences in seq.el
2018-12-23 2:04 ` Yuan Fu
@ 2018-12-23 14:34 ` Stefan Monnier
2019-01-08 4:12 ` Yuan Fu
0 siblings, 1 reply; 6+ messages in thread
From: Stefan Monnier @ 2018-12-23 14:34 UTC (permalink / raw)
To: Yuan Fu; +Cc: emacs-devel
>> (seq-concatenate 'list (seq-subseq seq 0 n)
>> (list element)
>> (seq-subseq seq n))
>
> That returns a list, not the sequence.
I'm not sure which sequence is "the" sequence, but a list is
definitely a sequence.
Stefan
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Generic sequences in seq.el
2018-12-23 14:34 ` Stefan Monnier
@ 2019-01-08 4:12 ` Yuan Fu
2019-01-08 15:01 ` Stefan Monnier
0 siblings, 1 reply; 6+ messages in thread
From: Yuan Fu @ 2019-01-08 4:12 UTC (permalink / raw)
To: Stefan Monnier; +Cc: emacs-devel
[-- Attachment #1: Type: text/plain, Size: 623 bytes --]
You are right. I can implement my own
method to return objects that are not vector, string or list.
Still, it would be convenient to have a function
that can destructively modify a sequence. What is
the reason to not have one?
Yuan
On Sun, Dec 23, 2018 at 9:34 AM Stefan Monnier <monnier@iro.umontreal.ca>
wrote:
> >> (seq-concatenate 'list (seq-subseq seq 0 n)
> >> (list element)
> >> (seq-subseq seq n))
> >
> > That returns a list, not the sequence.
>
> I'm not sure which sequence is "the" sequence, but a list is
> definitely a sequence.
>
>
> Stefan
>
[-- Attachment #2: Type: text/html, Size: 1623 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Generic sequences in seq.el
2019-01-08 4:12 ` Yuan Fu
@ 2019-01-08 15:01 ` Stefan Monnier
0 siblings, 0 replies; 6+ messages in thread
From: Stefan Monnier @ 2019-01-08 15:01 UTC (permalink / raw)
To: emacs-devel
> Still, it would be convenient to have a function
> that can destructively modify a sequence. What is
> the reason to not have one?
Two reasons:
1- Side-effects are bad.
2- There hasn't yet been a compelling reason to add one.
3- As mentioned before "We currently don't have any data-structure that
can be efficiently modified in-place via push/pop".
Stefan
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2019-01-08 15:01 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-12-21 11:42 Generic sequences in seq.el Yuan Fu
2018-12-21 14:24 ` Stefan Monnier
2018-12-23 2:04 ` Yuan Fu
2018-12-23 14:34 ` Stefan Monnier
2019-01-08 4:12 ` Yuan Fu
2019-01-08 15:01 ` Stefan Monnier
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.