Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to flatten a one-level-deep list?

16 views
Skip to first unread message

Marcin Borkowski

unread,
May 18, 2016, 1:13:19 AM5/18/16
to Help Gnu Emacs mailing list
Hi,

I have a list of lists of atoms, and I want to have a flat list
containing these atoms. I could use -flatten from dash.el, but I'd
prefer not to introduce such a dependency for this one function alone.
Is there anything *in core Emacs* to do it, or should I just write my
own version?

TIA,

--
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University

to...@tuxteam.de

unread,
May 18, 2016, 4:21:39 AM5/18/16
to help-gn...@gnu.org
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wed, May 18, 2016 at 07:12:56AM +0200, Marcin Borkowski wrote:
> Hi,
>
> I have a list of lists of atoms, and I want to have a flat list
> containing these atoms. I could use -flatten from dash.el, but I'd
> prefer not to introduce such a dependency for this one function alone.
> Is there anything *in core Emacs* to do it, or should I just write my
> own version?

(setq l '((a b c) (d e f) (g h i)))
(apply 'append l)
=> (a b c d e f g h i)

No idea whether performance or edge cases match your requirements,
though :)

regards
- -- t
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlc8JgMACgkQBcgs9XrR2ka6eACfe2VkMvr3Y/asv+Zj+gv2RX3l
sYUAn1gNe9Uozr3NSG2JlmMwIjk9UFaK
=N+Pt
-----END PGP SIGNATURE-----

Marcin Borkowski

unread,
May 18, 2016, 5:37:37 AM5/18/16
to to...@tuxteam.de, help-gn...@gnu.org

On 2016-05-18, at 10:21, to...@tuxteam.de wrote:

> (setq l '((a b c) (d e f) (g h i)))
> (apply 'append l)
> => (a b c d e f g h i)
>
> No idea whether performance or edge cases match your requirements,
> though :)

Thanks, that looks fine. Meanwhile, I went with a mapconcat nested
within a mapconcat (the atoms I have are strings, and what I really need
is concatenation of all of them).

> regards
> - -- t

Best,

Pascal J. Bourguignon

unread,
May 18, 2016, 1:52:25 PM5/18/16
to
<to...@tuxteam.de> writes:

> On Wed, May 18, 2016 at 07:12:56AM +0200, Marcin Borkowski wrote:
>> Hi,
>>
>> I have a list of lists of atoms, and I want to have a flat list
>> containing these atoms. I could use -flatten from dash.el, but I'd
>> prefer not to introduce such a dependency for this one function alone.
>> Is there anything *in core Emacs* to do it, or should I just write my
>> own version?
>
> (setq l '((a b c) (d e f) (g h i)))
> (apply 'append l)
> => (a b c d e f g h i)
>
> No idea whether performance or edge cases match your requirements,
> though :)

append will refer to the last list in that literal list, thus making the
result half literal. This may be a problem or not, depending on how you
use this result.

You can use concatenate (cl-concatenate) instead, if you want to copy
even the last subsequence:

(apply 'concatenate 'list l)
--> (a b c d e f g h i)


--
__Pascal Bourguignon__ http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk

Emanuel Berg

unread,
May 18, 2016, 5:08:12 PM5/18/16
to help-gn...@gnu.org
Marcin Borkowski <mb...@mbork.pl> writes:

>> (setq l '((a b c) (d e f) (g h i))) (apply 'append l) => (a b c d e f g h i)
>> No idea whether performance or edge cases match your requirements, though :)
>
> Thanks, that looks fine. Meanwhile, I went
> with a mapconcat nested within a mapconcat
> (the atoms I have are strings, and what
> I really need is concatenation of all of
> them).

C-h a flatten RET anyone?

(defun message-flatten-list (list)
"Return a new, flat list that contains all elements of LIST.

\(message-flatten-list '(1 (2 3 (4 5 (6))) 7))
=> (1 2 3 4 5 6 7)"
(cond ((consp list)
(apply 'append (mapcar 'message-flatten-list list)))
(list
(list list))))

--
underground experts united .... http://user.it.uu.se/~embe8573
Emacs Gnus Blogomatic ......... http://user.it.uu.se/~embe8573/blogomatic
- so far: 33 Blogomatic articles -


Stefan Monnier

unread,
May 20, 2016, 8:20:30 AM5/20/16
to help-gn...@gnu.org
> C-h a flatten RET anyone?

`flatten' is not a (one-level-deep) list operation, but a tree operation.


Stefan


Michael Heerdegen

unread,
May 20, 2016, 11:01:45 AM5/20/16
to help-gn...@gnu.org
How would you call the proposed functionality then?

I'm asking because I incidentally need a name for the analog stream
operation I want to add to stream.el:

#+begin_src emacs-lisp
(defun stream-??? (stream-of-streams)
"Concatenate all streams in STREAM-OF-STREAMS an return the result.
All elements in STREAM-OF-STREAMS must be streams. The result is
always a stream."
(stream-reduce #'stream-append (stream-empty) stream-of-streams))
#+end_src

(`stream-reduce' will be part of the patch; `stream-append' is already
existing in stream.el and has signature (&rest streams).)


Thanks,

Michael.


Marcin Borkowski

unread,
May 20, 2016, 1:20:46 PM5/20/16
to Michael Heerdegen, help-gn...@gnu.org

On 2016-05-20, at 17:01, Michael Heerdegen <michael_...@web.de> wrote:

> How would you call the proposed functionality then?
>
> I'm asking because I incidentally need a name for the analog stream
> operation I want to add to stream.el:
>
> #+begin_src emacs-lisp
> (defun stream-??? (stream-of-streams)
> "Concatenate all streams in STREAM-OF-STREAMS an return the result.
> All elements in STREAM-OF-STREAMS must be streams. The result is
> always a stream."
> (stream-reduce #'stream-append (stream-empty) stream-of-streams))
> #+end_src

Concatenate? ;-)

> Thanks,
>
> Michael.

Michael Heerdegen

unread,
May 20, 2016, 1:54:55 PM5/20/16
to Marcin Borkowski, help-gn...@gnu.org
Marcin Borkowski <mb...@mbork.pl> writes:

> Concatenate? ;-)

Yes, I thought about that name. But I would expect an (&rest streams)
signature for a function named `stream-concatenate'. `stream-flatten'
would be a better name in this regard, since it's clear that it
processes one stream. But it is problematic as well because it's
normally a tree function, as Stefan mentioned.


Thanks,

Michael.

Marcin Borkowski

unread,
May 20, 2016, 3:51:22 PM5/20/16
to Michael Heerdegen, help-gn...@gnu.org
stream-flatten-shallow, then?

Michael Heerdegen

unread,
May 20, 2016, 6:40:58 PM5/20/16
to Marcin Borkowski, help-gn...@gnu.org
Marcin Borkowski <mb...@mbork.pl> writes:

> stream-flatten-shallow, then?

Better, but hmm, quite longish.

In Haskell, the analog operation for lists seems to be named just
"concat".

Browsing thesauri suggests as alternatives "chain", "join" and "splice".

Maybe I just go with "concat" or "concatenate".


Thanks,

Michael.

0 new messages