Pivot¶
Let’s say you have the following 2 vectors:
[1 3 5 7]
[2 4 6 8]
How would you the following result?
[ [1 2]
[3 4]
[5 6]
[7 8] ]
Use interleave and partition¶
interleave zips the two collections and flattens the result:
(interleave [1 3 5 7] [2 4 6 8]) ;; => (1 2 3 4 5 6 7 8)
partition creates collections every n steps:
(partition [1 2 3 4 5 6 7 8]) ;; => ((1 2) (3 4) (5 6) (7 8))
Using them both:
(->> (list [1 3 5 7] [2 4 6 8] )
(apply interleave)
(partition 2)
)
;; => ((1 2) (3 4) (5 6) (7 8))
(->> (list [1 3 5 7] [2 4 6 8] )
(apply interleave)
(partition 2)
)
;; => [[1 2] [3 4] [5 6] [7 8]]
Use map with multiple collections¶
Alternatively, map can take more than one collection as an argument:
(map list [1 3 5 7] [2 4 6 8]) ;; => ((1 2) (3 4) (5 6) (7 8))