flatten

added
1.2

ns
clojure.core

type
function

(flatten x)

Takes any nested combination of sequential things (lists, vectors,
etc.) and returns their contents as a single, flat sequence.
(flatten nil) returns an empty sequence.

                user=> (flatten [1 [2 3]])
(1 2 3)

user=> (flatten '(1 2 3))
(1 2 3)

user=> (flatten '(1 2 [3 (4 5)])) 
(1 2 3 4 5)

user=> (flatten nil)
()

; Attention with stuff which is not a sequence

user=> (flatten 5)
()

user=> (flatten {:name "Hubert" :age 23})
()

; Workaround for maps

user=> (flatten (seq {:name "Hubert" :age 23}))
(:name "Hubert" :age 23)
            
                ;; Useful snippet: "merge" two or more vectors with `(comp vec flatten vector)`
(let [a [{:a "hi"} {:b "hey"}]
      b [{:c "yo"} {:d "hiya"}]
      c {:e ["hola" "bonjour"]}]
  ((comp vec flatten vector) a b c))
;;=> [{:a "hi"} {:b "hey"} {:c "yo"} {:d "hiya"} {:e ["hola" "bonjour"]}]