* How to flatten a one-level-deep list?
@ 2016-05-18 5:12 Marcin Borkowski
2016-05-18 8:21 ` tomas
[not found] ` <mailman.46.1463559697.6543.help-gnu-emacs@gnu.org>
0 siblings, 2 replies; 11+ messages in thread
From: Marcin Borkowski @ 2016-05-18 5:12 UTC (permalink / raw)
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
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: How to flatten a one-level-deep list?
2016-05-18 5:12 How to flatten a one-level-deep list? Marcin Borkowski
@ 2016-05-18 8:21 ` tomas
2016-05-18 9:37 ` Marcin Borkowski
[not found] ` <mailman.46.1463559697.6543.help-gnu-emacs@gnu.org>
1 sibling, 1 reply; 11+ messages in thread
From: tomas @ 2016-05-18 8:21 UTC (permalink / raw)
To: help-gnu-emacs
-----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-----
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: How to flatten a one-level-deep list?
2016-05-18 8:21 ` tomas
@ 2016-05-18 9:37 ` Marcin Borkowski
2016-05-18 21:07 ` Emanuel Berg
0 siblings, 1 reply; 11+ messages in thread
From: Marcin Borkowski @ 2016-05-18 9:37 UTC (permalink / raw)
To: tomas; +Cc: help-gnu-emacs
On 2016-05-18, at 10:21, tomas@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,
--
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: How to flatten a one-level-deep list?
[not found] ` <mailman.46.1463559697.6543.help-gnu-emacs@gnu.org>
@ 2016-05-18 17:52 ` Pascal J. Bourguignon
0 siblings, 0 replies; 11+ messages in thread
From: Pascal J. Bourguignon @ 2016-05-18 17:52 UTC (permalink / raw)
To: help-gnu-emacs
<tomas@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
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: How to flatten a one-level-deep list?
2016-05-18 9:37 ` Marcin Borkowski
@ 2016-05-18 21:07 ` Emanuel Berg
2016-05-20 12:17 ` Stefan Monnier
0 siblings, 1 reply; 11+ messages in thread
From: Emanuel Berg @ 2016-05-18 21:07 UTC (permalink / raw)
To: help-gnu-emacs
Marcin Borkowski <mbork@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 -
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: How to flatten a one-level-deep list?
2016-05-18 21:07 ` Emanuel Berg
@ 2016-05-20 12:17 ` Stefan Monnier
2016-05-20 15:01 ` Michael Heerdegen
0 siblings, 1 reply; 11+ messages in thread
From: Stefan Monnier @ 2016-05-20 12:17 UTC (permalink / raw)
To: help-gnu-emacs
> C-h a flatten RET anyone?
`flatten' is not a (one-level-deep) list operation, but a tree operation.
Stefan
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: How to flatten a one-level-deep list?
2016-05-20 12:17 ` Stefan Monnier
@ 2016-05-20 15:01 ` Michael Heerdegen
2016-05-20 17:20 ` Marcin Borkowski
0 siblings, 1 reply; 11+ messages in thread
From: Michael Heerdegen @ 2016-05-20 15:01 UTC (permalink / raw)
To: help-gnu-emacs
Stefan Monnier <monnier@iro.umontreal.ca> writes:
> > C-h a flatten RET anyone?
>
> `flatten' is not a (one-level-deep) list operation, but a tree
> operation.
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.
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: How to flatten a one-level-deep list?
2016-05-20 15:01 ` Michael Heerdegen
@ 2016-05-20 17:20 ` Marcin Borkowski
2016-05-20 17:54 ` Michael Heerdegen
0 siblings, 1 reply; 11+ messages in thread
From: Marcin Borkowski @ 2016-05-20 17:20 UTC (permalink / raw)
To: Michael Heerdegen; +Cc: help-gnu-emacs
On 2016-05-20, at 17:01, Michael Heerdegen <michael_heerdegen@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.
Best,
--
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: How to flatten a one-level-deep list?
2016-05-20 17:20 ` Marcin Borkowski
@ 2016-05-20 17:54 ` Michael Heerdegen
2016-05-20 19:51 ` Marcin Borkowski
0 siblings, 1 reply; 11+ messages in thread
From: Michael Heerdegen @ 2016-05-20 17:54 UTC (permalink / raw)
To: Marcin Borkowski; +Cc: help-gnu-emacs
Marcin Borkowski <mbork@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.
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: How to flatten a one-level-deep list?
2016-05-20 17:54 ` Michael Heerdegen
@ 2016-05-20 19:51 ` Marcin Borkowski
2016-05-20 22:14 ` Michael Heerdegen
0 siblings, 1 reply; 11+ messages in thread
From: Marcin Borkowski @ 2016-05-20 19:51 UTC (permalink / raw)
To: Michael Heerdegen; +Cc: help-gnu-emacs
On 2016-05-20, at 19:54, Michael Heerdegen <michael_heerdegen@web.de> wrote:
> Marcin Borkowski <mbork@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.
stream-flatten-shallow, then?
> Thanks,
>
> Michael.
Best,
--
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: How to flatten a one-level-deep list?
2016-05-20 19:51 ` Marcin Borkowski
@ 2016-05-20 22:14 ` Michael Heerdegen
0 siblings, 0 replies; 11+ messages in thread
From: Michael Heerdegen @ 2016-05-20 22:14 UTC (permalink / raw)
To: Marcin Borkowski; +Cc: help-gnu-emacs
Marcin Borkowski <mbork@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.
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2016-05-20 22:14 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-05-18 5:12 How to flatten a one-level-deep list? Marcin Borkowski
2016-05-18 8:21 ` tomas
2016-05-18 9:37 ` Marcin Borkowski
2016-05-18 21:07 ` Emanuel Berg
2016-05-20 12:17 ` Stefan Monnier
2016-05-20 15:01 ` Michael Heerdegen
2016-05-20 17:20 ` Marcin Borkowski
2016-05-20 17:54 ` Michael Heerdegen
2016-05-20 19:51 ` Marcin Borkowski
2016-05-20 22:14 ` Michael Heerdegen
[not found] ` <mailman.46.1463559697.6543.help-gnu-emacs@gnu.org>
2016-05-18 17:52 ` Pascal J. Bourguignon
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.