all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Basil L. Contovounesios" <contovob@tcd.ie>
To: Alex Branham <alex.branham@gmail.com>
Cc: 33309@debbugs.gnu.org, Michael Albinus <michael.albinus@gmx.de>,
	Stefan Monnier <monnier@IRO.UMontreal.CA>
Subject: bug#33309: Add flatten-list?
Date: Mon, 10 Dec 2018 22:42:14 +0000	[thread overview]
Message-ID: <87tvjl80mx.fsf@tcd.ie> (raw)
In-Reply-To: <87sgz5m98k.fsf@gmail.com> (Alex Branham's message of "Mon, 10 Dec 2018 14:12:43 -0600")

[-- Attachment #1: Type: text/plain, Size: 575 bytes --]

[Sorry, Alex, for sending this to you twice - I accidentally made my
 last message a narrow, rather than wide, reply.]

Alex Branham <alex.branham@gmail.com> writes:

> Thanks for the feedback, everyone.

Thanks for working on this.

> Here's a patch that implements `flatten-tree' which always returns a
> list and recurses into conses.

Given Emacs' recursive limitations, wouldn't an iterative implementation
be better?  For instance, the following currently blows
max-specpdl-size:

  (length (flatten-tree (make-list 800 nil)))

How about something like the following?


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: flatten-tree.diff --]
[-- Type: text/x-diff, Size: 851 bytes --]

diff --git a/lisp/subr.el b/lisp/subr.el
index f7eac75305..3fed3bc436 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -5453,10 +5453,15 @@ flatten-tree
 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)"
-  (cond ((null tree) nil)
-        ((consp tree) (append (flatten-tree (car tree))
-                              (flatten-tree (cdr tree))))
-        (t (list tree))))
+  (let (elems)
+    (setq tree (list tree))
+    (while (let ((elem (pop tree)))
+             (cond ((consp elem)
+                    (setq tree (cons (car elem) (cons (cdr elem) tree))))
+                   (elem
+                    (push elem elems)))
+             tree))
+    (nreverse elems)))
 
 ;; Technically, `flatten-list' is a misnomer, but we provide it here
 ;; for discoverability:

[-- Attachment #3: Type: text/plain, Size: 11 bytes --]


-- 
Basil

  parent reply	other threads:[~2018-12-10 22:42 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-07 19:46 bug#33309: Add flatten-list? Alex Branham
2018-11-07 20:36 ` Drew Adams
2018-11-07 21:19   ` Alex Branham
2018-12-10  8:44     ` Michael Albinus
2018-12-10 17:49       ` Stefan Monnier
2018-12-10 20:12         ` Alex Branham
2018-12-10 21:36           ` Stefan Monnier
2018-12-10 23:06             ` Alex Branham
2018-12-11 12:36               ` Stefan Monnier
2018-12-10 22:42           ` Basil L. Contovounesios [this message]
2018-12-10 23:17             ` Alex Branham
2018-12-10 23:26               ` Basil L. Contovounesios
2018-12-10 23:34               ` Stephen Berman
2018-12-11  8:21               ` Michael Albinus
2018-12-11  8:34               ` martin rudalics
2018-12-11 17:36 ` bug#33309: [PATCH] flatten-list Alex Branham
2018-12-11 20:11   ` Michael Albinus
2018-12-11 20:16     ` Alex Branham
2018-12-17 11:33       ` Michael Albinus

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87tvjl80mx.fsf@tcd.ie \
    --to=contovob@tcd.ie \
    --cc=33309@debbugs.gnu.org \
    --cc=alex.branham@gmail.com \
    --cc=michael.albinus@gmx.de \
    --cc=monnier@IRO.UMontreal.CA \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.