Question

My goal is to build a nested vector of dimension n consisting of a single element p. As an example let me choose n=2 and p=1, so the output would be:

   [[1 1] [1 1]]
Was it helpful?

Solution

Probably, you want something like this:

(defn square-matrix [n p]
  (->> p (repeat n) (repeat n)))

Or, if you need vectors (not seqs):

(defn square-matrix [n p]
  (->> p (repeat n) vec (repeat n) vec))

OTHER TIPS

I think what you want is (->> p (repeat n) vec (repeat n) vec).

(defn vec-of-dim [n e]
  (->> (repeat n e)
       (into [])
       (repeat n)
       (into [])))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top