yjcyxky
10/17/2019 - 2:22 PM

Flatten a Sequence

Difficulty: Easy

Topics: seqs core-functions

Write a function which flattens a sequence.

(= (__ '((1 2) 3 [4 [5 6]])) '(1 2 3 4 5 6))

(= (__ ["a" ["b"] "c"]) '("a" "b" "c"))

(= (__ '((((:a))))) '(:a))
(fn flatten-seq [nested-seq]
    (let [first-item (first nested-seq)
          rest-items (rest nested-seq)]
    (cond
        (nil? first-item)
        []
        (coll? first-item) 
        (concat (flatten-seq first-item) (flatten-seq rest-items))
        :default (concat [first-item] (flatten-seq rest-items)))))