clojureclojure-contrib

Building a nested vector in Clojure


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]]

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))