bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#33309: Add flatten-list?


From: Stefan Monnier
Subject: bug#33309: Add flatten-list?
Date: Mon, 10 Dec 2018 16:36:13 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

> -(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 via email to

[Prev in Thread] Current Thread [Next in Thread]