[英]Clojure reduce function for vector containing mixture vectors and doubles
I would like sum a vector containing a mixture of doubles and vectors.我想求和一个包含双精度和向量混合的向量。 Something like,就像是,
[[1 2 1 [1 2 3]] [1 2 4 [1 1 1]]...]
That I would like to sum such that I get something like,我想总结一下,我得到类似的东西,
[212 12 444 [11 2 12]]
Is there an efficient way to do this using core clojure functions like reduce or map?有没有一种使用像reduce或map这样的核心clojure函数来做到这一点的有效方法?
I think that you want to have a function that adds a nested vector structure, so that (nvadd [1 2 1 [1 2 3]] [1 2 4 [1 1 1]])
evaluates to [2 4 5 [2 3 4]]
.我认为您想要一个添加嵌套向量结构的函数,以便(nvadd [1 2 1 [1 2 3]] [1 2 4 [1 1 1]])
评估为[2 4 5 [2 3 4]]
。 You can then reduce
your vector of such vectors with that function.然后,您可以使用该函数reduce
此类向量的向量。
(defn nvadd [a b]
(mapv #(if (vector? %1)
(nvadd %1 %2)
(+ %1 %2))
a b))
This assumes well-formed input.这假设输入格式正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.