unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@IRO.UMontreal.CA>
To: Alex Branham <alex.branham@gmail.com>
Cc: 33309@debbugs.gnu.org, Michael Albinus <michael.albinus@gmx.de>
Subject: bug#33309: Add flatten-list?
Date: Mon, 10 Dec 2018 16:36:13 -0500	[thread overview]
Message-ID: <jwva7ld13c2.fsf-monnier+INBOX@gnu.org> (raw)
In-Reply-To: <87sgz5m98k.fsf@gmail.com> (Alex Branham's message of "Mon, 10 Dec 2018 14:12:43 -0600")

> -(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





  reply	other threads:[~2018-12-10 21:36 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 [this message]
2018-12-10 23:06             ` Alex Branham
2018-12-11 12:36               ` Stefan Monnier
2018-12-10 22:42           ` Basil L. Contovounesios
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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=jwva7ld13c2.fsf-monnier+INBOX@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=33309@debbugs.gnu.org \
    --cc=alex.branham@gmail.com \
    --cc=michael.albinus@gmx.de \
    /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 public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).