I completed Exercise 43 on 4clojure the other day and tested some other solutions. In particular, it confused me.
The task will ask you to write a function that satisfies all of this:
(= (__ [1 2 3 4 5 6] 2) '((1 3 5) (2 4 6))) (= (__ (range 9) 3) '((0 3 6) (1 4 7) (2 5 8))) (= (__ (range 10) 5) '((0 5) (1 6) (2 7) (3 8) (4 9)))
My solution was as follows:
(fn [ln] (map #(map second %) (vals (group-by #(mod (first %) n) (map vector (iterate inc 0) l)))))
User himself had this solution: #(apply map list (partition %2 %1)) and I could not understand how this works.
Let the first problem work:
(= (__ [1 2 3 4 5 6] 2) '((1 3 5) (2 4 6)))
Well, (#(partition %2 %1) [1 2 3 4 5 6] 2) will give us ((1 2) (3 4) (5 6)) now how to apply map list on this will produce (1 3 5) (2 4 6)
source share