From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Marcin Borkowski Newsgroups: gmane.emacs.help Subject: Re: Easy to add with push but not to the end of a list Date: Tue, 29 Nov 2022 09:33:03 +0100 Message-ID: <87wn7ew474.fsf@mbork.pl> References: <878rju96i5.fsf@dataswamp.org> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="11325"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.1.0; emacs 29.0.50 Cc: tomas@tuxteam.de, help-gnu-emacs@gnu.org To: Heime Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Tue Nov 29 09:34:12 2022 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 1ozw4C-0002ip-JZ for geh-help-gnu-emacs@m.gmane-mx.org; Tue, 29 Nov 2022 09:34:12 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ozw3L-0004YM-9y; Tue, 29 Nov 2022 03:33:19 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ozw3G-0004Y9-UV for help-gnu-emacs@gnu.org; Tue, 29 Nov 2022 03:33:14 -0500 Original-Received: from mail.mojserwer.eu ([195.110.48.8]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ozw3E-0001wN-St for help-gnu-emacs@gnu.org; Tue, 29 Nov 2022 03:33:14 -0500 Original-Received: from localhost (localhost [127.0.0.1]) by mail.mojserwer.eu (Postfix) with ESMTP id 076E02271598; Tue, 29 Nov 2022 09:33:10 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at mail.mojserwer.eu Original-Received: from mail.mojserwer.eu ([127.0.0.1]) by localhost (mail.mojserwer.eu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id b0C8RKe0b2kr; Tue, 29 Nov 2022 09:33:06 +0100 (CET) Original-Received: from localhost (178235147143.dynamic-3-poz-k-0-1-0.vectranet.pl [178.235.147.143]) by mail.mojserwer.eu (Postfix) with ESMTPSA id 5208718B6729; Tue, 29 Nov 2022 09:33:06 +0100 (CET) In-reply-to: Received-SPF: pass client-ip=195.110.48.8; envelope-from=mbork@mbork.pl; helo=mail.mojserwer.eu 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, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=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.29 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-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.help:141235 Archived-At: On 2022-11-29, at 08:56, Heime wrote: > Programming has got nothing to do with engineering or craft. What is it, then? (I'm genuinely curious about your opinion.) >> Singly linked lists are a tool. They are simple, lightweight, >> and appropriate for keeping things in sequence, and for adding >> things at the front. Not at the end. > > Stefan mentioned using reverse to put it into another list. If needed > the execution time would not be much different than actually place > new elements at end of list. The difference is that you keep adding items at the front and then reverse the list /once/. Another pattern you might use is to ditch lists and keep the items in a temporary buffer, keeping point at its end (which is easy with `insert'), and then traverse the buffer to gather the results as needed. You might even want to keep them in a buffer as a printed representation of a list, and then read it, using the Elisp parser. (I don't know what function would help with that, but I'm pretty certain such a function exists.) Not sure how efficient this would be, but I'd guess it could be pretty good with really large number of items. Yet another option would be to keep the whole list in some variable, but use another variable (say, `last') as a "pointer" to the last cons of that list. Then, you can define `fast-append' like this: (defvar last nil) (defvar my-list nil) (defun fast-append (el) "Append EL at the end of `my-list' in O(1) time." (if (not my-list) (setf my-list (cons el nil) last my-list) (setf (cdr last) (cons el nil) last (cdr last)))) This then would be O(1). >> Core library functions express idioms. They are expected to >> help people to find patterns on how to use tools appropriately >> (it's not much different from tools: a screwdriver has a >> handle, which spells "grip me here, please"). >> >> Adding a function to core (say `hsup') which suggests that >> it is as easy to push something at the end of a list would >> be misleading people to hold the screwdriver (or the knife!) >> at the wrong end and hurt themselves. > > Nobody will get hurt. Just inefficient for long lists or for > large number of calls. Every time someone appends to the end of a singly linked list, a cute kitty dies... ;-) -- Marcin Borkowski http://mbork.pl