* Re: master d62766305a: Add `take` and `ntake` (bug#56521) [not found] ` <20220717154726.F2455C00095@vcs2.savannah.gnu.org> @ 2022-07-18 8:48 ` Robert Pluim 2022-07-18 10:59 ` Mattias Engdegård 2022-07-18 17:21 ` Sam Steingold 1 sibling, 1 reply; 8+ messages in thread From: Robert Pluim @ 2022-07-18 8:48 UTC (permalink / raw) To: emacs-devel; +Cc: Mattias Engdegård >>>>> On Sun, 17 Jul 2022 11:47:25 -0400 (EDT), Mattias EngdegÃ¥rd <mattiase@acm.org> said: Mattias> +@defun ntake n list Mattias> +This is a version of @code{take} that works by destructively modifying Mattias> +the list structure of the argument. That makes it faster, but the Mattias> +original value of @var{list} is lost. Mattias> +@end defun Mattias> + Mattias> @defun last list &optional n Mattias> This function returns the last link of @var{list}. The @code{car} of Mattias> this link is the list's last element. If @var{list} is Mattias> null, Does this need the usual blurb to do (setq list-var (ntake 3 list-var)) to ensure `list-var' is updated? Mattias> +DEFUN ("take", Ftake, Stake, 2, 2, 0, Mattias> + doc: /* Return the first N elements of LIST. Mattias> +If N is zero or negative, return nil. Mattias> +If LIST is no more than N elements long, return it (or a copy). */) Mattias> + (Lisp_Object n, Lisp_Object list) I think this would be clearer as "If N is >= the length of LIST, return LIST (or a copy)." since weʼre talking about the effect of various values of N. Mattias> + Mattias> +DEFUN ("ntake", Fntake, Sntake, 2, 2, 0, Mattias> + doc: /* Modify LIST to keep only the first N elements. Mattias> +If N is zero or negative, return nil. Mattias> +If LIST is no more than N elements long, return it. */) And similarly here. Robert -- ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: master d62766305a: Add `take` and `ntake` (bug#56521) 2022-07-18 8:48 ` master d62766305a: Add `take` and `ntake` (bug#56521) Robert Pluim @ 2022-07-18 10:59 ` Mattias Engdegård 2022-07-18 11:55 ` Robert Pluim 0 siblings, 1 reply; 8+ messages in thread From: Mattias Engdegård @ 2022-07-18 10:59 UTC (permalink / raw) To: Robert Pluim; +Cc: emacs-devel 18 juli 2022 kl. 10.48 skrev Robert Pluim <rpluim@gmail.com>: > Does this need the usual blurb to do > > (setq list-var (ntake 3 list-var)) > > to ensure `list-var' is updated? Sort of; I added some text to that effect. Using the return value is only needed if N can be nonpositive. > I think this would be clearer as > > "If N is >= the length of LIST, return LIST (or a copy)." > > since weʼre talking about the effect of various values of N. Right, it's now been changed to something like that. Thank you! Now `seq-take` uses `take`, and so does `seq-subseq`. This made both faster, but the latter was boosted immensely by not formatting an error string up-front. `seq-subseq` would be even faster if simply written as (take (- end start) (nthcdr start sequence)) but the interface requirements force a much more cumbersome implementation. (And does it really need `copy-sequence`? The docs are unclear.) I suppose that `seq-subseq` should use `cl-defmethod` for dispatching on type but I didn't change that part. ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: master d62766305a: Add `take` and `ntake` (bug#56521) 2022-07-18 10:59 ` Mattias Engdegård @ 2022-07-18 11:55 ` Robert Pluim 2022-07-18 16:34 ` Michael Heerdegen 0 siblings, 1 reply; 8+ messages in thread From: Robert Pluim @ 2022-07-18 11:55 UTC (permalink / raw) To: Mattias Engdegård; +Cc: emacs-devel >>>>> On Mon, 18 Jul 2022 12:59:51 +0200, Mattias Engdegård <mattiase@acm.org> said: Mattias> 18 juli 2022 kl. 10.48 skrev Robert Pluim <rpluim@gmail.com>: >> Does this need the usual blurb to do >> >> (setq list-var (ntake 3 list-var)) >> >> to ensure `list-var' is updated? Mattias> Sort of; I added some text to that effect. Using the return value is only needed if N can be nonpositive. >> I think this would be clearer as >> >> "If N is >= the length of LIST, return LIST (or a copy)." >> >> since weʼre talking about the effect of various values of N. Mattias> Right, it's now been changed to something like that. Thank you! That looks good, thanks Mattias> Now `seq-take` uses `take`, and so does `seq-subseq`. This made both Mattias> faster, but the latter was boosted immensely by not formatting an Mattias> error string up-front. (meta-comment: youʼve made a number of speedups recently, it would be great if you could show some numbers of the effects) (meta-meta-comment: this is not a criticism of your work, just a nice-to-have) Mattias> `seq-subseq` would be even faster if simply written as Mattias> (take (- end start) (nthcdr start sequence)) Mattias> but the interface requirements force a much more cumbersome Mattias> implementation. (And does it really need `copy-sequence`? The docs are Mattias> unclear.) (info "(elisp) Sequence Functions") says: All functions defined in this library are free of side-effects; i.e., they do not modify any sequence (list, vector, or string) that you pass as an argument. It doesnʼt say anything about the result not sharing structure with the inputs. Probably code in the wild already implicitly depends on the `copy-sequence', though, so Iʼd think twice before modifying that contract. Robert -- ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: master d62766305a: Add `take` and `ntake` (bug#56521) 2022-07-18 11:55 ` Robert Pluim @ 2022-07-18 16:34 ` Michael Heerdegen 0 siblings, 0 replies; 8+ messages in thread From: Michael Heerdegen @ 2022-07-18 16:34 UTC (permalink / raw) To: emacs-devel Robert Pluim <rpluim@gmail.com> writes: > (info "(elisp) Sequence Functions") says: > > All functions defined in this library are free of side-effects; i.e., > they do not modify any sequence (list, vector, or string) that you pass > as an argument. > > It doesnʼt say anything about the result not sharing structure with > the inputs. The doc of `cl-subseq' which is (now) implemented based on `seq-subseq' does, however (info "(cl) Sequence Functions"): | [...] The return value is always a copy; it does not share structure | with SEQUENCE. Michael. ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: master d62766305a: Add `take` and `ntake` (bug#56521) [not found] ` <20220717154726.F2455C00095@vcs2.savannah.gnu.org> 2022-07-18 8:48 ` master d62766305a: Add `take` and `ntake` (bug#56521) Robert Pluim @ 2022-07-18 17:21 ` Sam Steingold 2022-07-18 18:56 ` Philip Kaludercic 1 sibling, 1 reply; 8+ messages in thread From: Sam Steingold @ 2022-07-18 17:21 UTC (permalink / raw) To: emacs-devel, Mattias EngdegÃ¥rd > * Mattias EngdegÃ¥rd <znggvnfr@npz.bet> [2022-07-17 11:47:25 -0400]: > > branch: master > commit d62766305ad8fe6ca1695341c34b9836d051e3cb > Author: Mattias Engdegård <mattiase@acm.org> > Commit: Mattias Engdegård <mattiase@acm.org> > > Add `take` and `ntake` (bug#56521) > > These are useful list primitives, complementary to `nthcdr`. How are these better than the venerable butlast and nbutlast? https://www.gnu.org/software/emacs/manual/html_node/elisp/List-Elements.html#index-butlast note that butlast and nbutlast have a very long Lisp history, they appear also in Common Lisp and many lisps before that. -- Sam Steingold (http://sds.podval.org/) on darwin Ns 10.3.2113 http://childpsy.net http://calmchildstories.com http://steingoldpsychology.com https://thereligionofpeace.com https://ffii.org https://jihadwatch.org Democrats, get out of my wallet! Republicans, get out of my bedroom! ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: master d62766305a: Add `take` and `ntake` (bug#56521) 2022-07-18 17:21 ` Sam Steingold @ 2022-07-18 18:56 ` Philip Kaludercic 2022-07-18 22:57 ` Sam Steingold 0 siblings, 1 reply; 8+ messages in thread From: Philip Kaludercic @ 2022-07-18 18:56 UTC (permalink / raw) To: emacs-devel; +Cc: Mattias EngdegÃ¥rd Sam Steingold <sds@gnu.org> writes: >> * Mattias EngdegÃ¥rd <znggvnfr@npz.bet> [2022-07-17 11:47:25 -0400]: >> >> branch: master >> commit d62766305ad8fe6ca1695341c34b9836d051e3cb >> Author: Mattias Engdegård <mattiase@acm.org> >> Commit: Mattias Engdegård <mattiase@acm.org> >> >> Add `take` and `ntake` (bug#56521) >> >> These are useful list primitives, complementary to `nthcdr`. > > How are these better than the venerable butlast and nbutlast? > https://www.gnu.org/software/emacs/manual/html_node/elisp/List-Elements.html#index-butlast > note that butlast and nbutlast have a very long Lisp history, they > appear also in Common Lisp and many lisps before that. The immediate advantage I recognise is that you don't need to determine the length of the list if you are actually just interested in the first n, not just not-interested in the last m. ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: master d62766305a: Add `take` and `ntake` (bug#56521) 2022-07-18 18:56 ` Philip Kaludercic @ 2022-07-18 22:57 ` Sam Steingold 2022-07-19 10:20 ` Mattias Engdegård 0 siblings, 1 reply; 8+ messages in thread From: Sam Steingold @ 2022-07-18 22:57 UTC (permalink / raw) To: emacs-devel, Philip Kaludercic > * Philip Kaludercic <cuvyvcx@cbfgrb.arg> [2022-07-18 18:56:24 +0000]: > > Sam Steingold <sds@gnu.org> writes: > >>> * Mattias EngdegÃ¥rd <znggvnfr@npz.bet> [2022-07-17 11:47:25 -0400]: >>> >>> branch: master >>> commit d62766305ad8fe6ca1695341c34b9836d051e3cb >>> Author: Mattias Engdegård <mattiase@acm.org> >>> Commit: Mattias Engdegård <mattiase@acm.org> >>> >>> Add `take` and `ntake` (bug#56521) >>> >>> These are useful list primitives, complementary to `nthcdr`. >> >> How are these better than the venerable butlast and nbutlast? >> https://www.gnu.org/software/emacs/manual/html_node/elisp/List-Elements.html#index-butlast >> note that butlast and nbutlast have a very long Lisp history, they >> appear also in Common Lisp and many lisps before that. > > The immediate advantage I recognise is that you don't need to determine > the length of the list if you are actually just interested in the first > n, not just not-interested in the last m. good point, thank you! in that case you might consider modifying (n)butlast to call (n)take to make them faster. -- Sam Steingold (http://sds.podval.org/) on darwin Ns 10.3.2113 http://childpsy.net http://calmchildstories.com http://steingoldpsychology.com https://www.memritv.org https://fairforall.org https://ffii.org Incorrect time synchronization. ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: master d62766305a: Add `take` and `ntake` (bug#56521) 2022-07-18 22:57 ` Sam Steingold @ 2022-07-19 10:20 ` Mattias Engdegård 0 siblings, 0 replies; 8+ messages in thread From: Mattias Engdegård @ 2022-07-19 10:20 UTC (permalink / raw) To: sds; +Cc: emacs-devel, Philip Kaludercic 19 juli 2022 kl. 00.57 skrev Sam Steingold <sds@gnu.org>: > you might consider modifying (n)butlast to > call (n)take to make them faster. Thank you, that was indeed the plan and has now been done (for `butlast`; it was difficult to see any benefit for `nbutlast`). ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2022-07-19 10:20 UTC | newest] Thread overview: 8+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- [not found] <165807284522.13427.2673143161208860104@vcs2.savannah.gnu.org> [not found] ` <20220717154726.F2455C00095@vcs2.savannah.gnu.org> 2022-07-18 8:48 ` master d62766305a: Add `take` and `ntake` (bug#56521) Robert Pluim 2022-07-18 10:59 ` Mattias Engdegård 2022-07-18 11:55 ` Robert Pluim 2022-07-18 16:34 ` Michael Heerdegen 2022-07-18 17:21 ` Sam Steingold 2022-07-18 18:56 ` Philip Kaludercic 2022-07-18 22:57 ` Sam Steingold 2022-07-19 10:20 ` Mattias Engdegård
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).