0

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]]
sunspots
  • 1,047
  • 13
  • 29

3 Answers3

4

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))
Leonid Beschastny
  • 50,364
  • 10
  • 118
  • 122
4

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

Chuck
  • 234,037
  • 30
  • 302
  • 389
0
(defn vec-of-dim [n e]
  (->> (repeat n e)
       (into [])
       (repeat n)
       (into [])))
mishadoff
  • 10,719
  • 2
  • 33
  • 55