Reputation: 417
I am really new to clojure! How does `mapcat work?
Upvotes: 3
Views: 1405
Reputation: 51500
mapcat
function is just a shortcut for applying concat
function to the result of map
function:
=> (mapcat reverse [[3 2 1 0] [6 5 4] [9 8 7]])
(0 1 2 3 4 5 6 7 8 9)
=> (apply concat (map reverse [[3 2 1 0] [6 5 4] [9 8 7]]))
(0 1 2 3 4 5 6 7 8 9)
By using mapcat
in combination with vector
function you can interlace several collections:
=> (mapcat vector [1 2 3 4 5 6] [:q :w :e :r :t :y])
(1 :q 2 :w 3 :e 4 :r 5 :t 6 :y)
You'll get the same result using list
function instead of vector
.
Upvotes: 8