Creating a seq from multiple seqs

Hi, I’ve been dabbling with Clojure for quite some time now, never done anything serious with it. I’ve come from the Smalltalk environment and never wanted to get into Java but when I found Clojure I thought I’d give it a try. The app that I’m building collects data from several sources, filters it and then reports on it.
So far so good, I can retrieve the data but trying to bring it together I’m stuck.
Basically I have one seq and a list of seqs and I want to bring the content of the list of seqs into the first seq. I’ve tried into, map, for etc but whatever I tried it ends up putting the list as a whole into the first seq as opposed to the items within the list. For example:

(def first [{:a 1} {:a 2}])
(def rest {[{:a 3} {:a 4}] [{:a 5} {:a 6}]})
(into first (???? rest)
;; => [{:a 1} {:a 2} {:a 3} {:a 4} {:a 5} {:a 6}]

It works if I do

(into first (first rest))

but I want to iterate over the second list so that all the lines are added to first.
Anybody any idea?
Thanks for reading!

This is not a list? Also you probably shouldn’t name your entites neither first nor rest.

Assuming you have a vector of the stuff you want to concatenate on the first vector, you could do:

(def some-things [{:a 1} {:a 2}])
(def more-things [[{:a 3} {:a 4}] [{:a 5} {:a 6}]])
(concat some-things (flatten more-things))
=> ({:a 1} {:a 2} {:a 3} {:a 4} {:a 5} {:a 6})

But someone will tell us that whenever you use flatten you have done something wrong. :slight_smile:

You should be able to use apply concat or mapcat identity.

Hi @PEZ, Thanks, #flatten did the job. In the many tutorials that I did/read I had come across it but forgotten all about it. @didibus - I’m going to check #mapcat - sounds interesting!
Great language, great group. Thanks!

2 Likes

Mostly flatten is deep, while if you only wanted to concat rest you would do:

(into first (apply concat rest))

You can also use the cat transducer together with the three-arity of into.

(into some-things cat more-things) ;=> [{:a 1} {:a 2} {:a 3} {:a 4} {:a 5} {:a 6}]
4 Likes

It’s pure beauty.

(20-char limit padding)

Another variation:

user=> (reduce into some-things more-things)
[{:a 1} {:a 2} {:a 3} {:a 4} {:a 5} {:a 6}]
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.