Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does mapcat work?

I am really new to clojure! How does `mapcat work?

like image 679
Mauricio Estevez Avatar asked May 16 '26 04:05

Mauricio Estevez


1 Answers

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)

References:

  • official Clojure API docs
  • clojuredocs.org website

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.

like image 100
Leonid Beschastny Avatar answered May 19 '26 02:05

Leonid Beschastny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!