Best way to repeatedly display values ​​from sequence to arg list?

Sorry for the noob question, but there is a good way to break down values ​​from such a sequence.

(somefunc [[a b c] [1 2 3 4 5 6 7 8 9]] (prn a b c))

.. when abc is assigned to values ​​until the sequence is exhausted and calls me the args function? The dose requires splitting the correct size.

(doseq [[a b c] (partition 3 [1 2 3 4 5 6 7 8 9])] (prn a b c))

Conclusion:

1 2 3 4 5 6 7 8 9

This does what I want, but it seems that there should be a way to do it in a straightforward manner without specifying a section. I found a solution with loop / recur, but this is a lot more code and clearly not idiomatic. What a good way to do this? Thank!

+3
source share
5 answers
(defn apply-to-three [f [a b c & xs]]
  (f a b c)
  (when xs
    (recur f xs)))

user=> (apply-to-three prn [1 2 3 4 5 6 7 8 9])
1 2 3
4 5 6
7 8 9
nil
+4
source
(defn some-func
  [[a b c & rest :as all]]
  (prn a b c)
  (prn rest)
  (prn all)

user> (some-func [1 2 3 4 5 6 7])
1 2 3
(4 5 6 7)
[1 2 3 4 5 6 7]

: http://java.ociweb.com/mark/clojure/article.html#Destructuring

+2

:

user=> (def coll [1 2 3 4 5 6 7 8 9 10])
#'user/coll
user=> (loop [[a b c & more] coll]
user=*   (when a
user=*     (prn a b c)
user=*     (recur more)))
1 2 3
4 5 6
7 8 9
10 nil nil
nil

when c .

+2

:

(map prn (partition 3 [1 2 3 4 5 6 7 8 9]))
0

+ - , . , doall: http://onclojure.com/2009/03/04/dorun-doseq-doall/

user> (dorun (map #(apply prn %) (partition 3 [1 2 3 4 5 6 7 8 9])))
1 2 3
4 5 6
7 8 9
nil
0

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


All Articles