* Appending to a list @ 2020-12-13 20:46 steve-humphreys 2020-12-13 21:03 ` Joost Kremers 0 siblings, 1 reply; 32+ messages in thread From: steve-humphreys @ 2020-12-13 20:46 UTC (permalink / raw) To: Help Gnu Emacs Would appending to list like this be good? (setq bird '("Toucan" "King Fisher")) (setq bird (append bird ("Swift") )) Or does one customarily use other constructs for adding to a list? ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-13 20:46 Appending to a list steve-humphreys @ 2020-12-13 21:03 ` Joost Kremers 2020-12-13 21:30 ` Óscar Fuentes 2020-12-13 21:41 ` steve-humphreys 0 siblings, 2 replies; 32+ messages in thread From: Joost Kremers @ 2020-12-13 21:03 UTC (permalink / raw) To: steve-humphreys; +Cc: help-gnu-emacs On Sun, Dec 13 2020, steve-humphreys@gmx.com wrote: > Would appending to list like this be good? > > (setq bird '("Toucan" "King Fisher")) > (setq bird (append bird ("Swift") )) This should result in an error, because `("Swift")` isn't quoted. You'd need: (setq bird (append bird '("Swift"))) But note that the last argument of `append` isn't copied, so it may not be a good idea to use a literal list. > Or does one customarily use other constructs for > adding to a list? `push` is what I would use: (push "Swift" bird) No need to use `setq` here. `push` won't check if the element is already in the list, though. You can use `cl-pushnew` in that case, but be sure to load the library it's in: (require 'cl-lib) (cl-pushnew "Swift" bird) HTH -- Joost Kremers Life has its moments ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-13 21:03 ` Joost Kremers @ 2020-12-13 21:30 ` Óscar Fuentes 2020-12-13 21:46 ` steve-humphreys ` (2 more replies) 2020-12-13 21:41 ` steve-humphreys 1 sibling, 3 replies; 32+ messages in thread From: Óscar Fuentes @ 2020-12-13 21:30 UTC (permalink / raw) To: help-gnu-emacs Joost Kremers <joostkremers@fastmail.fm> writes: > On Sun, Dec 13 2020, steve-humphreys@gmx.com wrote: >> Would appending to list like this be good? >> >> (setq bird '("Toucan" "King Fisher")) >> (setq bird (append bird ("Swift") )) > > This should result in an error, because `("Swift")` isn't quoted. You'd need: > > (setq bird (append bird '("Swift"))) > > But note that the last argument of `append` isn't copied, so it may not be a > good idea to use a literal list. > >> Or does one customarily use other constructs for >> adding to a list? > > `push` is what I would use: > > (push "Swift" bird) This does not append. > No need to use `setq` here. > > `push` won't check if the element is already in the list, though. You can use > `cl-pushnew` in that case, but be sure to load the library it's in: > > (require 'cl-lib) > (cl-pushnew "Swift" bird) Isn't that the same as (add-to-list bird "Swift" t) ? ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-13 21:30 ` Óscar Fuentes @ 2020-12-13 21:46 ` steve-humphreys 2020-12-13 22:37 ` Joost Kremers 2020-12-13 22:43 ` Stefan Monnier 2 siblings, 0 replies; 32+ messages in thread From: steve-humphreys @ 2020-12-13 21:46 UTC (permalink / raw) To: Óscar Fuentes; +Cc: help-gnu-emacs > Sent: Sunday, December 13, 2020 at 10:30 PM > From: "Óscar Fuentes" <ofv@wanadoo.es> > To: help-gnu-emacs@gnu.org > Subject: Re: Appending to a list > > Joost Kremers <joostkremers@fastmail.fm> writes: > > > On Sun, Dec 13 2020, steve-humphreys@gmx.com wrote: > >> Would appending to list like this be good? > >> > >> (setq bird '("Toucan" "King Fisher")) > >> (setq bird (append bird ("Swift") )) Can one just append lists together, then store the result to one of the variables, as I did? Or could such an approach lead to unintended consequences in some circumstances? Regards Steve > > This should result in an error, because `("Swift")` isn't quoted. You'd need: > > > > (setq bird (append bird '("Swift"))) > > > > But note that the last argument of `append` isn't copied, so it may not be a > > good idea to use a literal list. > > > >> Or does one customarily use other constructs for > >> adding to a list? > > > > `push` is what I would use: > > > > (push "Swift" bird) > > This does not append. > > > No need to use `setq` here. > > > > `push` won't check if the element is already in the list, though. You can use > > `cl-pushnew` in that case, but be sure to load the library it's in: > > > > (require 'cl-lib) > > (cl-pushnew "Swift" bird) > > Isn't that the same as > > (add-to-list bird "Swift" t) > > ? > > > ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-13 21:30 ` Óscar Fuentes 2020-12-13 21:46 ` steve-humphreys @ 2020-12-13 22:37 ` Joost Kremers 2020-12-13 23:08 ` Joost Kremers 2020-12-13 22:43 ` Stefan Monnier 2 siblings, 1 reply; 32+ messages in thread From: Joost Kremers @ 2020-12-13 22:37 UTC (permalink / raw) To: Óscar Fuentes; +Cc: help-gnu-emacs On Sun, Dec 13 2020, Óscar Fuentes wrote: > Joost Kremers <joostkremers@fastmail.fm> writes: >> On Sun, Dec 13 2020, steve-humphreys@gmx.com wrote: >>> Or does one customarily use other constructs for >>> adding to a list? >> >> `push` is what I would use: >> >> (push "Swift" bird) > > This does not append. Not in the Elisp sense, no. But in e.g., Python, `append` is the function that's used to add an element to a list and the OP indicated that that's what he was after. >> (require 'cl-lib) >> (cl-pushnew "Swift" bird) > > Isn't that the same as > > (add-to-list bird "Swift" t) The doc string of `add-to-list` says that it should really only be used on configuration variables in one's init file. For some reason (that I admittedly do not understand), `add-to-list` shouldn't be used on lexical variables. -- Joost Kremers Life has its moments ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-13 22:37 ` Joost Kremers @ 2020-12-13 23:08 ` Joost Kremers 0 siblings, 0 replies; 32+ messages in thread From: Joost Kremers @ 2020-12-13 23:08 UTC (permalink / raw) Cc: Óscar Fuentes, help-gnu-emacs On Sun, Dec 13 2020, Joost Kremers wrote: > On Sun, Dec 13 2020, Óscar Fuentes wrote: >> Joost Kremers <joostkremers@fastmail.fm> writes: >>> On Sun, Dec 13 2020, steve-humphreys@gmx.com wrote: >>>> Or does one customarily use other constructs for >>>> adding to a list? >>> >>> `push` is what I would use: >>> >>> (push "Swift" bird) >> >> This does not append. > > Not in the Elisp sense, no. But in e.g., Python, `append` is the function that's > used to add an element to a list and the OP indicated that that's what he was > after. Hmm, of course you meant "it doesn't add the new element to the end of the list"... I thought you were referring to the fact that in Elisp, `append` combines lists into a single list. -- Joost Kremers Life has its moments ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-13 21:30 ` Óscar Fuentes 2020-12-13 21:46 ` steve-humphreys 2020-12-13 22:37 ` Joost Kremers @ 2020-12-13 22:43 ` Stefan Monnier 2020-12-13 23:25 ` steve-humphreys 2 siblings, 1 reply; 32+ messages in thread From: Stefan Monnier @ 2020-12-13 22:43 UTC (permalink / raw) To: help-gnu-emacs >> `push` is what I would use: >> (push "Swift" bird) > > This does not append. Indeed, and it's a feature: append costs O(n), both in immediate CPU time and in memory allocations (which can increase memory use or at least increase the time spent in the GC) where `n` is the length of the list to which you append. So if you do that O(n) times, you get an overall O(n²) complexity. IOW, in most cases you're really better off with pushing to the beginning of the list, and if the order doesn't suit you, then use `reverse` afterwards, which will still be O(n) overall rather than O(n²). >> No need to use `setq` here. >> >> `push` won't check if the element is already in the list, though. You can use >> `cl-pushnew` in that case, but be sure to load the library it's in: >> >> (require 'cl-lib) >> (cl-pushnew "Swift" bird) > Isn't that the same as > (add-to-list bird "Swift" t) Not quite: - your `t` at the end makes `add-to-list` add to the rear of the list whereas `cl-pushnew` adds it to the front. - `add-to-list` would require you to quote the variable symbol, as in: (add-to-list 'bird "Swift" t) - `add-to-list` cannot operate on a lexically scoped `bird` variable. - `add-to-list` cannot operate on generalized variables, whereas `cl-pushnew` (just like `push`) will be happy with things like (cl-pushnew "Swift" (gethash "birds" table)) - `cl-pushnew` by default compares with `eql` whereas here you'd likely want to compare with `equal`, like `add-to-list` does, so you'd need: (cl-pushnew "Swift" bird :test #'equal) BTW `push` is significantly faster than either of `add-to-list` or `cl-pushnew` since it doesn't need to check in O(n) time if the element is already on the list. Stefan ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-13 22:43 ` Stefan Monnier @ 2020-12-13 23:25 ` steve-humphreys 2020-12-13 23:33 ` Emanuel Berg via Users list for the GNU Emacs text editor 0 siblings, 1 reply; 32+ messages in thread From: steve-humphreys @ 2020-12-13 23:25 UTC (permalink / raw) To: Stefan Monnier; +Cc: help-gnu-emacs > Sent: Sunday, December 13, 2020 at 11:43 PM > From: "Stefan Monnier" <monnier@iro.umontreal.ca> > To: help-gnu-emacs@gnu.org > Subject: Re: Appending to a list > > >> `push` is what I would use: > >> (push "Swift" bird) > > > > This does not append. > > Indeed, and it's a feature: append costs O(n), both in immediate CPU > time and in memory allocations (which can increase memory use or at > least increase the time spent in the GC) where `n` is the length of the > list to which you append. So if you do that O(n) times, you get an > overall O(n²) complexity. I don't care the way I insert. If inserting in the beginning is more efficient I would do that.. > IOW, in most cases you're really better off with pushing to the > beginning of the list, and if the order doesn't suit you, then use > `reverse` afterwards, which will still be O(n) overall rather than > O(n²). > > >> No need to use `setq` here. > >> > >> `push` won't check if the element is already in the list, though. You can use > >> `cl-pushnew` in that case, but be sure to load the library it's in: > >> > >> (require 'cl-lib) > >> (cl-pushnew "Swift" bird) > > Isn't that the same as > > (add-to-list bird "Swift" t) > > Not quite: > > - your `t` at the end makes `add-to-list` add to the rear of the > list whereas `cl-pushnew` adds it to the front. > - `add-to-list` would require you to quote the variable symbol, as in: > > (add-to-list 'bird "Swift" t) > > - `add-to-list` cannot operate on a lexically scoped `bird` variable. > > - `add-to-list` cannot operate on generalized variables, whereas > `cl-pushnew` (just like `push`) will be happy with things like > > (cl-pushnew "Swift" (gethash "birds" table)) > > - `cl-pushnew` by default compares with `eql` whereas here you'd likely > want to compare with `equal`, like `add-to-list` does, so you'd need: > > (cl-pushnew "Swift" bird :test #'equal) > > BTW `push` is significantly faster than either of `add-to-list` or > `cl-pushnew` since it doesn't need to check in O(n) time if the > element is already on the list. I am thinking using push if there are only few caveats when using it, compared to the other ones. > Stefan > > > ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-13 23:25 ` steve-humphreys @ 2020-12-13 23:33 ` Emanuel Berg via Users list for the GNU Emacs text editor 2020-12-14 0:19 ` steve-humphreys 0 siblings, 1 reply; 32+ messages in thread From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-13 23:33 UTC (permalink / raw) To: help-gnu-emacs steve-humphreys wrote: > I don't care the way I insert. If inserting in the beginning > is more efficient I would do that These two sentences are inconsistent :) -- underground experts united http://user.it.uu.se/~embe8573 https://dataswamp.org/~incal ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-13 23:33 ` Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-14 0:19 ` steve-humphreys 2020-12-14 0:37 ` Emanuel Berg via Users list for the GNU Emacs text editor 0 siblings, 1 reply; 32+ messages in thread From: steve-humphreys @ 2020-12-14 0:19 UTC (permalink / raw) To: moasenwood; +Cc: help-gnu-emacs > Sent: Monday, December 14, 2020 at 12:33 AM > From: "Emanuel Berg via Users list for the GNU Emacs text editor" <help-gnu-emacs@gnu.org> > To: help-gnu-emacs@gnu.org > Subject: Re: Appending to a list > > steve-humphreys wrote: > > > I don't care the way I insert. If inserting in the beginning > > is more efficient I would do that > > These two sentences are inconsistent :) Could have made a good politician then! I don't mind inserting at the beginning if that makes things more efficient. :) > -- > underground experts united > http://user.it.uu.se/~embe8573 > https://dataswamp.org/~incal > > > ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-14 0:19 ` steve-humphreys @ 2020-12-14 0:37 ` Emanuel Berg via Users list for the GNU Emacs text editor 2020-12-14 2:54 ` steve-humphreys 0 siblings, 1 reply; 32+ messages in thread From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-14 0:37 UTC (permalink / raw) To: help-gnu-emacs steve-humphreys wrote: > I don't mind inserting at the beginning if that makes things > more efficient. :) In your particular use case it will probably never be an issue of efficiency but as a programmer I guess the more details you know and understand and can put to good use the better... -- underground experts united http://user.it.uu.se/~embe8573 https://dataswamp.org/~incal ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-14 0:37 ` Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-14 2:54 ` steve-humphreys 2020-12-14 2:58 ` Emanuel Berg via Users list for the GNU Emacs text editor 2020-12-14 4:07 ` Michael Heerdegen 0 siblings, 2 replies; 32+ messages in thread From: steve-humphreys @ 2020-12-14 2:54 UTC (permalink / raw) To: moasenwood; +Cc: help-gnu-emacs Consider I have the following two templates, and want a make a list named "tmplt-animal", which I can then pass to org-capture-templates. (setq org-capture-templates tmplt-animal) How can I use "push" to do that? -------- template code -------- (setq tmplt-bird `( ("1" "Toucan" entry (file "~/animal.org") ,(concat "* Toucan\n" " Species:\n" " Region: %?\n" " Brief:\n" " Entered: %T\n")) ("2" "King Fisher" entry (file "~/animal.org") ,(concat "* King Fisher" " Species:\n" " Region: %?\n" " Brief:\n" " Entered: %T\n")) )) (setq tmplt-Amphibian `( ("3" "Frog or Toad" entry (file "~/animal.org") ,(concat "* Frog or Toad\n" " Species:\n" " Region: %?\n" " Brief:\n" " Entered: %T\n")) ("4" "Salamander" entry (file "~/animal.org") ,(concat "* King Fisher" " Species:\n" " Region: %?\n" " Brief:\n" " Entered: %T\n")) )) > Sent: Monday, December 14, 2020 at 1:37 AM > From: "Emanuel Berg via Users list for the GNU Emacs text editor" <help-gnu-emacs@gnu.org> > To: help-gnu-emacs@gnu.org > Subject: Re: Appending to a list > > steve-humphreys wrote: > > > I don't mind inserting at the beginning if that makes things > > more efficient. :) > > In your particular use case it will probably never be an issue > of efficiency but as a programmer I guess the more details you > know and understand and can put to good use the better... > > -- > underground experts united > http://user.it.uu.se/~embe8573 > https://dataswamp.org/~incal > > > ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-14 2:54 ` steve-humphreys @ 2020-12-14 2:58 ` Emanuel Berg via Users list for the GNU Emacs text editor 2020-12-14 3:23 ` steve-humphreys 2020-12-14 4:07 ` Michael Heerdegen 1 sibling, 1 reply; 32+ messages in thread From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-14 2:58 UTC (permalink / raw) To: help-gnu-emacs steve-humphreys wrote: > (setq tmplt-bird `( > > ("1" "Toucan" entry > (file "~/animal.org") > ,(concat > "* Toucan\n" > " Species:\n" > " Region: %?\n" > " Brief:\n" > " Entered: %T\n")) [...] Darn, it is epidemic! Now you do that as well! What is that??? While it looks MUCH better than the other guy's it is still all over the place IMO. But to answer your question: no idea. :) -- underground experts united http://user.it.uu.se/~embe8573 https://dataswamp.org/~incal ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-14 2:58 ` Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-14 3:23 ` steve-humphreys 0 siblings, 0 replies; 32+ messages in thread From: steve-humphreys @ 2020-12-14 3:23 UTC (permalink / raw) To: moasenwood; +Cc: help-gnu-emacs > Sent: Monday, December 14, 2020 at 3:58 AM > From: "Emanuel Berg via Users list for the GNU Emacs text editor" <help-gnu-emacs@gnu.org> > To: help-gnu-emacs@gnu.org > Subject: Re: Appending to a list > > steve-humphreys wrote: > > > (setq tmplt-bird `( > > > > ("1" "Toucan" entry > > (file "~/animal.org") > > ,(concat > > "* Toucan\n" > > " Species:\n" > > " Region: %?\n" > > " Brief:\n" > > " Entered: %T\n")) [...] > > Darn, it is epidemic! Now you do that as well! What is that??? Coronavirus! :) > While it looks MUCH better than the other guy's it is still all > over the place IMO. > > But to answer your question: no idea. > > :) > > -- > underground experts united > http://user.it.uu.se/~embe8573 > https://dataswamp.org/~incal > > > ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-14 2:54 ` steve-humphreys 2020-12-14 2:58 ` Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-14 4:07 ` Michael Heerdegen 2020-12-14 4:16 ` Emanuel Berg via Users list for the GNU Emacs text editor 2020-12-14 4:23 ` steve-humphreys 1 sibling, 2 replies; 32+ messages in thread From: Michael Heerdegen @ 2020-12-14 4:07 UTC (permalink / raw) To: help-gnu-emacs steve-humphreys@gmx.com writes: > Consider I have the following two templates, and want a make a list > named "tmplt-animal", which I can then pass to org-capture-templates. > > (setq org-capture-templates tmplt-animal) > > How can I use "push" to do that? > > -------- template code -------- > [...] Seems your list is actually not an element to add that just happens to be a list, but a list of elements you want to add (but the elements are also lists again, ehem...) In this case you want to `append' the lists, not `push'. You may also define those bindings using `defvar' if you really want to set all the used variables at top level. Michael. ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-14 4:07 ` Michael Heerdegen @ 2020-12-14 4:16 ` Emanuel Berg via Users list for the GNU Emacs text editor 2020-12-14 4:23 ` steve-humphreys 1 sibling, 0 replies; 32+ messages in thread From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-14 4:16 UTC (permalink / raw) To: help-gnu-emacs Michael Heerdegen wrote: > In this case you want to `append' the lists, not `push'. > You may also define those bindings using `defvar' if you > really want to set all the used variables at top level. ... and to shut up the byte compiler :) -- underground experts united http://user.it.uu.se/~embe8573 https://dataswamp.org/~incal ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-14 4:07 ` Michael Heerdegen 2020-12-14 4:16 ` Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-14 4:23 ` steve-humphreys 2020-12-14 4:50 ` Emanuel Berg via Users list for the GNU Emacs text editor 2020-12-14 17:22 ` Michael Heerdegen 1 sibling, 2 replies; 32+ messages in thread From: steve-humphreys @ 2020-12-14 4:23 UTC (permalink / raw) To: Michael Heerdegen; +Cc: help-gnu-emacs > Sent: Monday, December 14, 2020 at 5:07 AM > From: "Michael Heerdegen" <michael_heerdegen@web.de> > To: help-gnu-emacs@gnu.org > Subject: Re: Appending to a list > > steve-humphreys@gmx.com writes: > > > Consider I have the following two templates, and want a make a list > > named "tmplt-animal", which I can then pass to org-capture-templates. > > > > (setq org-capture-templates tmplt-animal) > > > > How can I use "push" to do that? > > > > -------- template code -------- > > [...] > > Seems your list is actually not an element to add that just happens to > be a list, but a list of elements you want to add (but the elements are > also lists again, ehem...) I thought I could push element by element. > In this case you want to `append' the lists, not `push'. You may also > define those bindings using `defvar' if you really want to set all the > used variables at top level. I got inspiration from some discussions here that push is more efficient and wanted to have a go. Yes, I do use push to make the larger list. Have not thought about defvar before. I have read that "setq" does not introduce a variable, but I would need some explanation about that. > Michael. > > > ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-14 4:23 ` steve-humphreys @ 2020-12-14 4:50 ` Emanuel Berg via Users list for the GNU Emacs text editor 2020-12-14 5:13 ` steve-humphreys ` (2 more replies) 2020-12-14 17:22 ` Michael Heerdegen 1 sibling, 3 replies; 32+ messages in thread From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-14 4:50 UTC (permalink / raw) To: help-gnu-emacs steve-humphreys wrote: > I thought I could push element by element. You can, you can push whatever, if that data structure makes it more involved solve it for onee pilot case, then write a small `defun', put it there, and ever after just pass the single element to that. > I got inspiration from some discussions here that push is > more efficient and wanted to have a go. Yes, I do use push to > make the larger list. Have not thought about defvar before. > I have read that "setq" does not introduce a variable, but > I would need some explanation about that. It works with `setq' only without `defvar' the byte-compiler, which should always be used BTW, without it the byte-compiler will complain. Comment-out the defvars in this (require 'cl-lib) (defvar bird-list-all) (defvar bird-list-1) (defvar bird-list-2) (setq bird-list-all '()) (setq bird-list-1 '("Western jackdaw" "European goldfinch")) (setq bird-list-2 '("rook")) (cl-pushnew bird-list-1 bird-list-all) ; (("Western jackdaw" "European goldfinch")) (cl-pushnew bird-list-2 bird-list-all) ; (("rook") ("Western jackdaw" "European goldfinch")) and the byte-compiler will say geh.el: In toplevel form: geh.el:193:7: Warning: assignment to free variable ‘bird-list-all’ geh.el:195:7: Warning: assignment to free variable ‘bird-list-1’ geh.el:196:7: Warning: assignment to free variable ‘bird-list-2’ geh.el:198:13: Warning: reference to free variable ‘bird-list-1’ geh.el:198:25: Warning: reference to free variable ‘bird-list-all’ geh.el:199:13: Warning: reference to free variable ‘bird-list-2’ In more ambitious projects, like a package, e.g. [1], defvar should be used also to make available documentation, both in the code and for the on-line help which in practice is the same thing - Emacs is self-documenting, remember? Heh, not really, right? But still, do it. Anyway, don't rely too much on variables, defvar or no defvar. Better to rely on defuns and use `let' and `let*' there. IMO! [1] https://dataswamp.org/~incal/emacs-init/buc.el -- underground experts united http://user.it.uu.se/~embe8573 https://dataswamp.org/~incal ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-14 4:50 ` Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-14 5:13 ` steve-humphreys 2020-12-14 6:20 ` Emanuel Berg via Users list for the GNU Emacs text editor 2020-12-14 5:28 ` steve-humphreys 2020-12-14 5:55 ` steve-humphreys 2 siblings, 1 reply; 32+ messages in thread From: steve-humphreys @ 2020-12-14 5:13 UTC (permalink / raw) To: moasenwood; +Cc: help-gnu-emacs > Sent: Monday, December 14, 2020 at 5:50 AM > From: "Emanuel Berg via Users list for the GNU Emacs text editor" <help-gnu-emacs@gnu.org> > To: help-gnu-emacs@gnu.org > Subject: Re: Appending to a list > > steve-humphreys wrote: > > > I thought I could push element by element. > > You can, you can push whatever, if that data structure makes it > more involved solve it for onee pilot case, then write a small > `defun', put it there, and ever after just pass the single > element to that. > > > I got inspiration from some discussions here that push is > > more efficient and wanted to have a go. Yes, I do use push to > > make the larger list. Have not thought about defvar before. > > I have read that "setq" does not introduce a variable, but > > I would need some explanation about that. > > It works with `setq' only without `defvar' the byte-compiler, > which should always be used BTW, without it the byte-compiler > will complain. > > Comment-out the defvars in this > > (require 'cl-lib) > > (defvar bird-list-all) > (defvar bird-list-1) > (defvar bird-list-2) > > (setq bird-list-all '()) > > (setq bird-list-1 '("Western jackdaw" "European goldfinch")) > (setq bird-list-2 '("rook")) > > (cl-pushnew bird-list-1 bird-list-all) ; (("Western jackdaw" "European goldfinch")) > (cl-pushnew bird-list-2 bird-list-all) ; (("rook") ("Western jackdaw" "European goldfinch")) > > and the byte-compiler will say > > geh.el: > In toplevel form: > geh.el:193:7: Warning: assignment to free variable ‘bird-list-all’ > geh.el:195:7: Warning: assignment to free variable ‘bird-list-1’ > geh.el:196:7: Warning: assignment to free variable ‘bird-list-2’ > geh.el:198:13: Warning: reference to free variable ‘bird-list-1’ > geh.el:198:25: Warning: reference to free variable ‘bird-list-all’ > geh.el:199:13: Warning: reference to free variable ‘bird-list-2’ > > In more ambitious projects, like a package, e.g. [1], defvar > should be used also to make available documentation, both in > the code and for the on-line help which in practice is the same > thing - Emacs is self-documenting, remember? > > Heh, not really, right? But still, do it. > > Anyway, don't rely too much on variables, defvar or no defvar. > Better to rely on defuns and use `let' and `let*' there. IMO! Yes, I have used "let" and "let*" to take a word and make frog over other words. > [1] https://dataswamp.org/~incal/emacs-init/buc.el > > -- > underground experts united > http://user.it.uu.se/~embe8573 > https://dataswamp.org/~incal > > > ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-14 5:13 ` steve-humphreys @ 2020-12-14 6:20 ` Emanuel Berg via Users list for the GNU Emacs text editor 2020-12-14 6:25 ` steve-humphreys 0 siblings, 1 reply; 32+ messages in thread From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-14 6:20 UTC (permalink / raw) To: help-gnu-emacs steve-humphreys wrote: > Yes, I have used "let" and "let*" to take a word and make > frog over other words. ? -- underground experts united http://user.it.uu.se/~embe8573 https://dataswamp.org/~incal ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-14 6:20 ` Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-14 6:25 ` steve-humphreys 0 siblings, 0 replies; 32+ messages in thread From: steve-humphreys @ 2020-12-14 6:25 UTC (permalink / raw) To: moasenwood; +Cc: help-gnu-emacs > Sent: Monday, December 14, 2020 at 7:20 AM > From: "Emanuel Berg via Users list for the GNU Emacs text editor" <help-gnu-emacs@gnu.org> > To: help-gnu-emacs@gnu.org > Subject: Re: Appending to a list > > steve-humphreys wrote: > > > Yes, I have used "let" and "let*" to take a word and make > > frog over other words. Made a defun to skip over words, and used let. Jump over words like a frog. > ? > > -- > underground experts united > http://user.it.uu.se/~embe8573 > https://dataswamp.org/~incal > > > ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-14 4:50 ` Emanuel Berg via Users list for the GNU Emacs text editor 2020-12-14 5:13 ` steve-humphreys @ 2020-12-14 5:28 ` steve-humphreys 2020-12-14 6:26 ` Emanuel Berg via Users list for the GNU Emacs text editor 2020-12-14 5:55 ` steve-humphreys 2 siblings, 1 reply; 32+ messages in thread From: steve-humphreys @ 2020-12-14 5:28 UTC (permalink / raw) To: moasenwood; +Cc: help-gnu-emacs > Sent: Monday, December 14, 2020 at 5:50 AM > From: "Emanuel Berg via Users list for the GNU Emacs text editor" <help-gnu-emacs@gnu.org> > To: help-gnu-emacs@gnu.org > Subject: Re: Appending to a list > > steve-humphreys wrote: > > > I thought I could push element by element. > > You can, you can push whatever, if that data structure makes it > more involved solve it for onee pilot case, then write a small > `defun', put it there, and ever after just pass the single > element to that. > > > I got inspiration from some discussions here that push is > > more efficient and wanted to have a go. Yes, I do use push to > > make the larger list. Have not thought about defvar before. > > I have read that "setq" does not introduce a variable, but > > I would need some explanation about that. > > It works with `setq' only without `defvar' the byte-compiler, > which should always be used BTW, without it the byte-compiler > will complain. I have not understood the paragraph above. > Comment-out the defvars in this > > (require 'cl-lib) > > (defvar bird-list-all) > (defvar bird-list-1) > (defvar bird-list-2) > > (setq bird-list-all '()) > > (setq bird-list-1 '("Western jackdaw" "European goldfinch")) > (setq bird-list-2 '("rook")) > > (cl-pushnew bird-list-1 bird-list-all) ; (("Western jackdaw" "European goldfinch")) > (cl-pushnew bird-list-2 bird-list-all) ; (("rook") ("Western jackdaw" "European goldfinch")) > > and the byte-compiler will say > > geh.el: > In toplevel form: > geh.el:193:7: Warning: assignment to free variable ‘bird-list-all’ > geh.el:195:7: Warning: assignment to free variable ‘bird-list-1’ > geh.el:196:7: Warning: assignment to free variable ‘bird-list-2’ > geh.el:198:13: Warning: reference to free variable ‘bird-list-1’ > geh.el:198:25: Warning: reference to free variable ‘bird-list-all’ > geh.el:199:13: Warning: reference to free variable ‘bird-list-2’ > > In more ambitious projects, like a package, e.g. [1], defvar > should be used also to make available documentation, both in > the code and for the on-line help which in practice is the same > thing - Emacs is self-documenting, remember? > > Heh, not really, right? But still, do it. > > Anyway, don't rely too much on variables, defvar or no defvar. > Better to rely on defuns and use `let' and `let*' there. IMO! > > [1] https://dataswamp.org/~incal/emacs-init/buc.el > > -- > underground experts united > http://user.it.uu.se/~embe8573 > https://dataswamp.org/~incal > > > ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-14 5:28 ` steve-humphreys @ 2020-12-14 6:26 ` Emanuel Berg via Users list for the GNU Emacs text editor 2020-12-14 10:25 ` steve-humphreys 0 siblings, 1 reply; 32+ messages in thread From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-14 6:26 UTC (permalink / raw) To: help-gnu-emacs steve-humphreys wrote: >> It works with `setq' only without `defvar' the >> byte-compiler, which should always be used BTW, without it >> the byte-compiler will complain. > > I have not understood the paragraph above. After you have written some Elisp, say fort-knox-access.el, use the byte-compiler like this $ emacs -batch -f batch-byte-compile fort-knox-access.el The byte-compiler will tell you how to improve your code and also when the output fort-knox-access.elc file is loaded in Emacs it will run faster to some extent. -- underground experts united http://user.it.uu.se/~embe8573 https://dataswamp.org/~incal ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-14 6:26 ` Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-14 10:25 ` steve-humphreys 0 siblings, 0 replies; 32+ messages in thread From: steve-humphreys @ 2020-12-14 10:25 UTC (permalink / raw) To: moasenwood; +Cc: help-gnu-emacs > Sent: Monday, December 14, 2020 at 7:26 AM > From: "Emanuel Berg via Users list for the GNU Emacs text editor" <help-gnu-emacs@gnu.org> > To: help-gnu-emacs@gnu.org > Subject: Re: Appending to a list > > steve-humphreys wrote: > > >> It works with `setq' only without `defvar' the > >> byte-compiler, which should always be used BTW, without it > >> the byte-compiler will complain. > > > > I have not understood the paragraph above. > > After you have written some Elisp, say fort-knox-access.el, use > the byte-compiler like this > > $ emacs -batch -f batch-byte-compile fort-knox-access.el > > The byte-compiler will tell you how to improve your code and > also when the output fort-knox-access.elc file is loaded in > Emacs it will run faster to some extent. I see. > -- > underground experts united > http://user.it.uu.se/~embe8573 > https://dataswamp.org/~incal > > > ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-14 4:50 ` Emanuel Berg via Users list for the GNU Emacs text editor 2020-12-14 5:13 ` steve-humphreys 2020-12-14 5:28 ` steve-humphreys @ 2020-12-14 5:55 ` steve-humphreys 2020-12-14 6:36 ` Emanuel Berg via Users list for the GNU Emacs text editor 2 siblings, 1 reply; 32+ messages in thread From: steve-humphreys @ 2020-12-14 5:55 UTC (permalink / raw) To: moasenwood; +Cc: help-gnu-emacs > Sent: Monday, December 14, 2020 at 5:50 AM > From: "Emanuel Berg via Users list for the GNU Emacs text editor" <help-gnu-emacs@gnu.org> > To: help-gnu-emacs@gnu.org > Subject: Re: Appending to a list > > steve-humphreys wrote: > > > I thought I could push element by element. > > You can, you can push whatever, if that data structure makes it > more involved solve it for onee pilot case, then write a small > `defun', put it there, and ever after just pass the single > element to that. > > > I got inspiration from some discussions here that push is > > more efficient and wanted to have a go. Yes, I do use push to > > make the larger list. Have not thought about defvar before. > > I have read that "setq" does not introduce a variable, but > > I would need some explanation about that. > > It works with `setq' only without `defvar' the byte-compiler, > which should always be used BTW, without it the byte-compiler > will complain. Do you mean that by commenting the defvars and using "setq", the compiler will complain because no variables were defined by bird-list*. defvar can only be assigned if they have not been set and have a documentation string. But when do you decide betweenh setq and defvar? > Comment-out the defvars in this > > (require 'cl-lib) > > (defvar bird-list-all) > (defvar bird-list-1) > (defvar bird-list-2) > > (setq bird-list-all '()) > > (setq bird-list-1 '("Western jackdaw" "European goldfinch")) > (setq bird-list-2 '("rook")) > > (cl-pushnew bird-list-1 bird-list-all) ; (("Western jackdaw" "European goldfinch")) > (cl-pushnew bird-list-2 bird-list-all) ; (("rook") ("Western jackdaw" "European goldfinch")) Is the problem with the compiler occurring because of cl-pushnew? > and the byte-compiler will say > > geh.el: > In toplevel form: > geh.el:193:7: Warning: assignment to free variable ‘bird-list-all’ > geh.el:195:7: Warning: assignment to free variable ‘bird-list-1’ > geh.el:196:7: Warning: assignment to free variable ‘bird-list-2’ > geh.el:198:13: Warning: reference to free variable ‘bird-list-1’ > geh.el:198:25: Warning: reference to free variable ‘bird-list-all’ > geh.el:199:13: Warning: reference to free variable ‘bird-list-2’ > > In more ambitious projects, like a package, e.g. [1], defvar > should be used also to make available documentation, both in > the code and for the on-line help which in practice is the same > thing - Emacs is self-documenting, remember? > > Heh, not really, right? But still, do it. > > Anyway, don't rely too much on variables, defvar or no defvar. > Better to rely on defuns and use `let' and `let*' there. IMO! > > [1] https://dataswamp.org/~incal/emacs-init/buc.el > > -- > underground experts united > http://user.it.uu.se/~embe8573 > https://dataswamp.org/~incal > > > ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-14 5:55 ` steve-humphreys @ 2020-12-14 6:36 ` Emanuel Berg via Users list for the GNU Emacs text editor 0 siblings, 0 replies; 32+ messages in thread From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-14 6:36 UTC (permalink / raw) To: help-gnu-emacs steve-humphreys wrote: > Do you mean that by commenting the defvars and using "setq", > the compiler will complain because no variables were defined > by bird-list*. Yes, removing or commenting out the defvars and then using `setq' that will happen. > defvar can only be assigned if they have not been set and > have a documentation string. Use `defvar' before setq. The init value is often a source of confusion... but use it by all means if it fancies you. The docstring is also optional. Use it in packages. In your own code, obviously you do whatever you want. Just (defvar pi) (setq pi 3.1415) is OK :) > But when do you decide betweenh setq and defvar? Use defvar first, then setq. But avoid it all if you can by using defuns and `let'/`let*'. > Is the problem with the compiler occurring because of > cl-pushnew? The byte-compiler is OK with `cl-pushnew' because of the previous (require 'cl-lib) line. -- underground experts united http://user.it.uu.se/~embe8573 https://dataswamp.org/~incal ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-14 4:23 ` steve-humphreys 2020-12-14 4:50 ` Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-14 17:22 ` Michael Heerdegen 2020-12-14 18:25 ` Emanuel Berg via Users list for the GNU Emacs text editor 1 sibling, 1 reply; 32+ messages in thread From: Michael Heerdegen @ 2020-12-14 17:22 UTC (permalink / raw) To: help-gnu-emacs steve-humphreys@gmx.com writes: > > Seems your list is actually not an element to add that just happens to > > be a list, but a list of elements you want to add (but the elements are > > also lists again, ehem...) > > I thought I could push element by element. Seems there is/was a misunderstanding: that you wanted to append an one-element list, which is apparently not the case. In the general case, `append' is of course not less efficient than pushing from one list to another element by element. Regards, Michael. ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-14 17:22 ` Michael Heerdegen @ 2020-12-14 18:25 ` Emanuel Berg via Users list for the GNU Emacs text editor 2020-12-15 0:17 ` steve-humphreys 0 siblings, 1 reply; 32+ messages in thread From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-14 18:25 UTC (permalink / raw) To: help-gnu-emacs Michael Heerdegen wrote: > Seems there is/was a misunderstanding: that you wanted to > append an one-element list, which is apparently not the case. > In the general case, `append' is of course not less efficient > than pushing from one list to another element by element. I don't understand the issue, even less than you because I don't even understand it enough to misunderstand it, _but_ I think the focus should be how to just solve it using standard and common methods, they will be efficient enough :) -- underground experts united http://user.it.uu.se/~embe8573 https://dataswamp.org/~incal ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-14 18:25 ` Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-15 0:17 ` steve-humphreys 2020-12-15 1:38 ` Emanuel Berg via Users list for the GNU Emacs text editor 0 siblings, 1 reply; 32+ messages in thread From: steve-humphreys @ 2020-12-15 0:17 UTC (permalink / raw) To: moasenwood; +Cc: help-gnu-emacs > Sent: Monday, December 14, 2020 at 7:25 PM > From: "Emanuel Berg via Users list for the GNU Emacs text editor" <help-gnu-emacs@gnu.org> > To: help-gnu-emacs@gnu.org > Subject: Re: Appending to a list > > Michael Heerdegen wrote: > > > Seems there is/was a misunderstanding: that you wanted to > > append an one-element list, which is apparently not the case. > > In the general case, `append' is of course not less efficient > > than pushing from one list to another element by element. > > I don't understand the issue, even less than you because > I don't even understand it enough to misunderstand it, _but_ > I think the focus should be how to just solve it using standard > and common methods, they will be efficient enough :) I got greedy. > -- > underground experts united > http://user.it.uu.se/~embe8573 > https://dataswamp.org/~incal > > > ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-15 0:17 ` steve-humphreys @ 2020-12-15 1:38 ` Emanuel Berg via Users list for the GNU Emacs text editor 0 siblings, 0 replies; 32+ messages in thread From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-15 1:38 UTC (permalink / raw) To: help-gnu-emacs steve-humphreys wrote: > I got greedy. Greedy algorithm: at every step of the way, choose the most rewarding next step. Usually works well but without the big picture it can lead to dead ends. But a good bet if you don't know what else to do. -- underground experts united http://user.it.uu.se/~embe8573 https://dataswamp.org/~incal ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-13 21:03 ` Joost Kremers 2020-12-13 21:30 ` Óscar Fuentes @ 2020-12-13 21:41 ` steve-humphreys 2020-12-13 23:31 ` Emanuel Berg via Users list for the GNU Emacs text editor 1 sibling, 1 reply; 32+ messages in thread From: steve-humphreys @ 2020-12-13 21:41 UTC (permalink / raw) To: Joost Kremers; +Cc: help-gnu-emacs > Sent: Sunday, December 13, 2020 at 10:03 PM > From: "Joost Kremers" <joostkremers@fastmail.fm> > To: steve-humphreys@gmx.com > Cc: help-gnu-emacs@gnu.org > Subject: Re: Appending to a list > > > On Sun, Dec 13 2020, steve-humphreys@gmx.com wrote: > > Would appending to list like this be good? > > > > (setq bird '("Toucan" "King Fisher")) > > (setq bird (append bird ("Swift") )) > > This should result in an error, because `("Swift")` isn't quoted. You'd need: > > (setq bird (append bird '("Swift"))) > > But note that the last argument of `append` isn't copied, so it may not be a > good idea to use a literal list. > > > Or does one customarily use other constructs for > > adding to a list? > > `push` is what I would use: > > (push "Swift" bird) > > No need to use `setq` here. > > `push` won't check if the element is already in the list, though. You can use > `cl-pushnew` in that case, but be sure to load the library it's in: > > (require 'cl-lib) > (cl-pushnew "Swift" bird) It won't be already in the list. Basically I just want to push entries that are themselves lists, and just go with push push push ... That was very helpful Joost. > HTH > > -- > Joost Kremers > Life has its moments > > ^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: Appending to a list 2020-12-13 21:41 ` steve-humphreys @ 2020-12-13 23:31 ` Emanuel Berg via Users list for the GNU Emacs text editor 0 siblings, 0 replies; 32+ messages in thread From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-13 23:31 UTC (permalink / raw) To: help-gnu-emacs steve-humphreys wrote: > Basically I just want to push entries that are themselves > lists, and just go with push push push ... You can use `cl-pushnew' for pushing lists as well! As in: (require 'cl-lib) (defvar bird-list-all) (defvar bird-list-1) (defvar bird-list-2) (setq bird-list-all '()) (setq bird-list-1 '("Western jackdaw" "European goldfinch")) (setq bird-list-2 '("rook")) (cl-pushnew bird-list-1 bird-list-all) ; (("Western jackdaw" "European goldfinch")) (cl-pushnew bird-list-2 bird-list-all) ; (("rook") ("Western jackdaw" "European goldfinch")) Birds and Elisp! That, I didn't see coming! Check out this file: https://dataswamp.org/~incal/BIRDS It even has some Elisp to compute the number of observed birds ("kryss", as they are called in Swedish). I have 61, that means not much of an ornithologist, I'm afraid :$ :) -- underground experts united http://user.it.uu.se/~embe8573 https://dataswamp.org/~incal ^ permalink raw reply [flat|nested] 32+ messages in thread
end of thread, other threads:[~2020-12-15 1:38 UTC | newest] Thread overview: 32+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2020-12-13 20:46 Appending to a list steve-humphreys 2020-12-13 21:03 ` Joost Kremers 2020-12-13 21:30 ` Óscar Fuentes 2020-12-13 21:46 ` steve-humphreys 2020-12-13 22:37 ` Joost Kremers 2020-12-13 23:08 ` Joost Kremers 2020-12-13 22:43 ` Stefan Monnier 2020-12-13 23:25 ` steve-humphreys 2020-12-13 23:33 ` Emanuel Berg via Users list for the GNU Emacs text editor 2020-12-14 0:19 ` steve-humphreys 2020-12-14 0:37 ` Emanuel Berg via Users list for the GNU Emacs text editor 2020-12-14 2:54 ` steve-humphreys 2020-12-14 2:58 ` Emanuel Berg via Users list for the GNU Emacs text editor 2020-12-14 3:23 ` steve-humphreys 2020-12-14 4:07 ` Michael Heerdegen 2020-12-14 4:16 ` Emanuel Berg via Users list for the GNU Emacs text editor 2020-12-14 4:23 ` steve-humphreys 2020-12-14 4:50 ` Emanuel Berg via Users list for the GNU Emacs text editor 2020-12-14 5:13 ` steve-humphreys 2020-12-14 6:20 ` Emanuel Berg via Users list for the GNU Emacs text editor 2020-12-14 6:25 ` steve-humphreys 2020-12-14 5:28 ` steve-humphreys 2020-12-14 6:26 ` Emanuel Berg via Users list for the GNU Emacs text editor 2020-12-14 10:25 ` steve-humphreys 2020-12-14 5:55 ` steve-humphreys 2020-12-14 6:36 ` Emanuel Berg via Users list for the GNU Emacs text editor 2020-12-14 17:22 ` Michael Heerdegen 2020-12-14 18:25 ` Emanuel Berg via Users list for the GNU Emacs text editor 2020-12-15 0:17 ` steve-humphreys 2020-12-15 1:38 ` Emanuel Berg via Users list for the GNU Emacs text editor 2020-12-13 21:41 ` steve-humphreys 2020-12-13 23:31 ` Emanuel Berg via Users list for the GNU Emacs text editor
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.