From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: steve-humphreys@gmx.com Newsgroups: gmane.emacs.help Subject: Re: Appending to a list Date: Mon, 14 Dec 2020 00:25:56 +0100 Message-ID: References: <877dpljtwg.fsf@fastmail.fm> <87sg89v1f7.fsf@telefonica.net> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="12208"; mail-complaints-to="usenet@ciao.gmane.io" Cc: help-gnu-emacs@gnu.org To: Stefan Monnier Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Mon Dec 14 00:26:43 2020 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1koalG-00033z-T9 for geh-help-gnu-emacs@m.gmane-mx.org; Mon, 14 Dec 2020 00:26:42 +0100 Original-Received: from localhost ([::1]:43318 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1koalF-00027k-Vc for geh-help-gnu-emacs@m.gmane-mx.org; Sun, 13 Dec 2020 18:26:41 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:48238) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1koakg-00027b-N4 for help-gnu-emacs@gnu.org; Sun, 13 Dec 2020 18:26:06 -0500 Original-Received: from mout.gmx.net ([212.227.15.15]:41187) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1koakb-0005d7-RD for help-gnu-emacs@gnu.org; Sun, 13 Dec 2020 18:26:06 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1607901956; bh=eK9ISlv2OQ76LdPz9eRMA8WoPi/SMGTqs1+5xVZo+3Y=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=X6Q3zt+mVo0X4S8OFVYxBXldts2hh80/+E4OzV161BPu3RqtsVBR3ztgfvSiipu6k RNRyJit4lDRVMgHH6mYE37CWoyT9O20BRsph5TqPR+WfI2J62YfeWjhiTzinRrRlcE DbU4pyV7wJ14XhG8LIC1TfsEAzoKvlh5meFcltro= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Original-Received: from [213.165.168.94] ([213.165.168.94]) by web-mail.gmx.net (3c-app-mailcom-bs09.server.lan [172.19.170.177]) (via HTTP); Mon, 14 Dec 2020 00:25:56 +0100 Importance: normal Sensitivity: Normal In-Reply-To: X-UI-Message-Type: mail X-Priority: 3 X-Provags-ID: V03:K1:QpK1v81OQbldEauF55s26Ddi9OQxzxZ6pz7Sd4YjHLDzYMWQwHNrIkqCp/JQQH/3VLO0Y SygehGLTYYSVjjeUpRKNBGVkI7JAOx8C5cVG7MR1rw/Ygdlb1dPwIMWHDKzxMyMVYog2bd+tTZT2 VWglU4a+qqI6w0Jdw8LGyFtc4jYhc4TLuEBteDvFYNuF/7aTLh9PChoDTCSeGTW0MqPGKtbQ1UzG vZQBvNY0AbyWDhZRF965joBVxIq6VYX1XbG5iHBuiEHhAyOsBpcLoD3ujfA3sVjR54GS2omi+JdI YE= X-UI-Out-Filterresults: notjunk:1;V03:K0:iHfQYGMK/K8=:l7vTSZChQnNordzSrcPrQw hdaPxq1ggeo1bLL0kuu1eCWiDiqkZdzDM9+EqrVTu9o7+d+Dm3zsn/ZD0boG48MuOKDlFRk17 7dqEpZFElYk9TreDXrgA/VE4MEK+UpcHnplbZKPVIhFck+r7qG8gLLtpVlhhUxuPcTNvf8Le2 j2OqCAuyAB2JKwizCbvm06OnUqWbyXD9HmLPFyZDWf7nm8boBHCm1xx03BYJ7pJ/jzuv3iOJN M7q361tvLv+sNtpi6L2CvZWmYw3sI+5rkMCdh1HP2ToROjkM5SlUP6cCKmS0+iUhtnSKXHkwW cc+35AwKOupEzKjBU8nhMcqdH2x12GOe0ggDJIv8Nua+ri4hHmtcBXZXbrxsexK9nEGj4K4xX cZ/2DYbSEGXD9PMv7gyb41gb21AJvVegci31J9XY/+Wg5Kk3PAbTjTi5AtoatQ9Ir/2DLKl+l 4NwhjzaHUEWHoCGvJe2aqKAhA07kJrFiYUlb92ConRx5baPlL0I4hquI4oXDXX73kcKQzvF3i X7SVFParkvKzk0Y8woHnauJ1I1qF+0wil+ZMHS90+BZzUzSbO8YFGWrnXt0N977mrv/DRUt4m t1chZnnNYvb+U= Received-SPF: pass client-ip=212.227.15.15; envelope-from=steve-humphreys@gmx.com; helo=mout.gmx.net X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:126334 Archived-At: > Sent: Sunday, December 13, 2020 at 11:43 PM > From: "Stefan Monnier" > To: help-gnu-emacs@gnu=2Eorg > Subject: Re: Appending to a list > > >> `push` is what I would use: > >> (push "Swift" bird) > > > > This does not append=2E >=20 > 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=2E So if you do that O(n) times, you get an > overall O(n=C2=B2) complexity=2E I don't care the way I insert=2E If inserting in the beginning is more efficient I would do that=2E=2E =20 =20 > 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=C2=B2)=2E >=20 > >> No need to use `setq` here=2E > >> > >> `push` won't check if the element is already in the list, though=2E Y= ou 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) >=20 > Not quite: >=20 > - your `t` at the end makes `add-to-list` add to the rear of the > list whereas `cl-pushnew` adds it to the front=2E > - `add-to-list` would require you to quote the variable symbol, as in: >=20 > (add-to-list 'bird "Swift" t) >=20 > - `add-to-list` cannot operate on a lexically scoped `bird` variable=2E >=20 > - `add-to-list` cannot operate on generalized variables, whereas > `cl-pushnew` (just like `push`) will be happy with things like >=20 > (cl-pushnew "Swift" (gethash "birds" table)) >=20 > - `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: >=20 > (cl-pushnew "Swift" bird :test #'equal) >=20 > 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=2E =20 I am thinking using push if there are only few caveats when using it, compared to the other ones=2E =20 > Stefan >=20 >=20 >