[英]Building a nested vector in Clojure
My goal is to build a nested vector of dimension n consisting of a single element p.我的目标是构建一个由单个元素 p 组成的 n 维嵌套向量。 As an example let me choose n=2 and p=1, so the output would be:
作为一个例子,让我选择 n=2 和 p=1,所以输出将是:
[[1 1] [1 1]]
Probably, you want something like this:可能你想要这样的东西:
(defn square-matrix [n p]
(->> p (repeat n) (repeat n)))
Or, if you need vectors (not seqs):或者,如果您需要向量(不是 seqs):
(defn square-matrix [n p]
(->> p (repeat n) vec (repeat n) vec))
我想你想要的是(->> p (repeat n) vec (repeat n) vec)
。
(defn vec-of-dim [n e]
(->> (repeat n e)
(into [])
(repeat n)
(into [])))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.