From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.bugs Subject: bug#33309: Add flatten-list? Date: Mon, 10 Dec 2018 16:36:13 -0500 Message-ID: References: <87r2fw7jsa.fsf@gmail.com> <058f4a0f-7ce4-49c4-ae54-0bc259bd82d1@default> <87pnvg7fgg.fsf@gmail.com> <8736r5ojnc.fsf@gmx.de> <87sgz5m98k.fsf@gmail.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1544477713 10463 195.159.176.226 (10 Dec 2018 21:35:13 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 10 Dec 2018 21:35:13 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) Cc: 33309@debbugs.gnu.org, Michael Albinus To: Alex Branham Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Mon Dec 10 22:35:08 2018 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gWTCm-0002b8-Kw for geb-bug-gnu-emacs@m.gmane.org; Mon, 10 Dec 2018 22:35:08 +0100 Original-Received: from localhost ([::1]:34858 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gWTEr-0006cv-DF for geb-bug-gnu-emacs@m.gmane.org; Mon, 10 Dec 2018 16:37:17 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:50274) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gWTEh-0006ce-Tg for bug-gnu-emacs@gnu.org; Mon, 10 Dec 2018 16:37:08 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gWTEc-0000do-UJ for bug-gnu-emacs@gnu.org; Mon, 10 Dec 2018 16:37:07 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:37950) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gWTEc-0000dY-Nv for bug-gnu-emacs@gnu.org; Mon, 10 Dec 2018 16:37:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1gWTEc-0000GF-HL for bug-gnu-emacs@gnu.org; Mon, 10 Dec 2018 16:37:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Stefan Monnier Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 10 Dec 2018 21:37:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 33309 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Original-Received: via spool by 33309-submit@debbugs.gnu.org id=B33309.1544477779951 (code B ref 33309); Mon, 10 Dec 2018 21:37:02 +0000 Original-Received: (at 33309) by debbugs.gnu.org; 10 Dec 2018 21:36:19 +0000 Original-Received: from localhost ([127.0.0.1]:42208 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gWTDv-0000FF-FT for submit@debbugs.gnu.org; Mon, 10 Dec 2018 16:36:19 -0500 Original-Received: from chene.dit.umontreal.ca ([132.204.246.20]:36852) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gWTDs-0000F4-I2 for 33309@debbugs.gnu.org; Mon, 10 Dec 2018 16:36:17 -0500 Original-Received: from pastel.home (lechon.iro.umontreal.ca [132.204.27.242]) by chene.dit.umontreal.ca (8.14.7/8.14.1) with ESMTP id wBALaDwO018381; Mon, 10 Dec 2018 16:36:13 -0500 Original-Received: by pastel.home (Postfix, from userid 20848) id 2790A6A4F8; Mon, 10 Dec 2018 16:36:13 -0500 (EST) In-Reply-To: <87sgz5m98k.fsf@gmail.com> (Alex Branham's message of "Mon, 10 Dec 2018 14:12:43 -0600") X-NAI-Spam-Flag: NO X-NAI-Spam-Threshold: 5 X-NAI-Spam-Score: 0 X-NAI-Spam-Rules: 2 Rules triggered EDT_SA_DN_PASS=0, RV6436=0 X-NAI-Spam-Version: 2.3.0.9418 : core <6436> : inlines <6980> : streams <1806756> : uri <2762967> X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:153313 Archived-At: > -(defun eshell-flatten-list (args) > - "Flatten any lists within ARGS, so that there are no sublists." > - (let ((new-list (list t))) > - (dolist (a args) > - (if (and (listp a) > - (listp (cdr a))) > - (nconc new-list (eshell-flatten-list a)) > - (nconc new-list (list a)))) > - (cdr new-list))) So this one leaves (1 . 2) alone: (5 nil (1 . 2)) ==> (5 (1 . 2)) but burps on (1 2 . 3) message-flatten-list would likely signal an error on (1 . 2). > -;; `lpr-flatten-list' is defined here (copied from "message.el" and > -;; enhanced to handle dotted pairs as well) until we can get some > -;; sensible autoloads, or `flatten-list' gets put somewhere decent. > - > -;; (lpr-flatten-list '((a . b) c (d . e) (f g h) i . j)) > -;; => (a b c d e f g h i j) > - > -(defun lpr-flatten-list (&rest list) > - (lpr-flatten-list-1 list)) > - > -(defun lpr-flatten-list-1 (list) > - (cond > - ((null list) nil) > - ((consp list) > - (append (lpr-flatten-list-1 (car list)) > - (lpr-flatten-list-1 (cdr list)))) > - (t (list list)))) This one treats car and cdr symetrically: (5 nil (1 . 2)) ==> (5 1 2) > -(defun tramp-compat-flatten-list (args) Copied from eshell-flatten-list, apparently. > -(defun js--flatten-list (list) > - (cl-loop for item in list > - nconc (cond ((consp item) > - (js--flatten-list item)) > - (item (list item))))) This one just drops the non-nil cdr: (5 nil (1 . 2)) ==> (5 1) > +(defun flatten-tree (tree) > + "Take TREE and \"flatten\" it. > +This always returns a list containing all the elements of TREE. > +\(flatten-tree \\='(1 (2 3 (4 5 (6))) 7)) > +=> (1 2 3 4 5 6 7)" I think we should document clearly what should happen with nil and with dotted pairs. > + (cond ((null tree) nil) > + ((consp tree) (append (flatten-tree (car tree)) > + (flatten-tree (cdr tree)))) > + (t (list tree)))) I think testing `null` after (rather than before) `consp` will be marginally more efficient. Stefan