Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I combine two vectors of vectors element-wise in clojure?

Suppose I've got:

'[[c c c]
  [y y y]
  [m m m]]

and

'[[r g b]
  [r g b]
  [r g b]]

and I'd like to have:

'[[[c,r] [c,g] [c,b]]
  [[y,r] [y,g] [y,b]]
  [[m,r] [m,g] [m,b]]]

What is the elegant way to do this in clojure?

like image 486
John Lawrence Aspden Avatar asked Feb 06 '13 19:02

John Lawrence Aspden


1 Answers

(def a '[[c c c]
         [y y y]
         [m m m]])
(def b '[[r g b]
         [r g b]
         [r g b]])

(mapv (partial mapv vector) a b) ;; will work with arbitrary number 
                                 ;; of equal sized arguments

;=> [[[c r] [c g] [c b]] [[y r] [y g] [y b]] [[m r] [m g] [m b]]]
like image 71
Alex Stoddard Avatar answered Nov 15 '22 05:11

Alex Stoddard