How does this function change the rotation process to x the number of subsequences

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)

+4
source share
1 answer

apply uses ((1 2) (3 4) (5 6)) as a list of optional arguments of variable length. The map iteration then applies the list function to all three of these additional lists.

As a result, it expands as follows:

  (apply map list '((1 2) (3 4) (5 6))) => (map list '(1 2) '(3 4) '(5 6)) => (list 1 3 5) and (list 2 4 6) 
+8
source

Source: https://habr.com/ru/post/1392589/


All Articles