Creating Matrices In Clojure

The following is the ugliest code I have ever written in any language. This happens to be written in Clojure and is intended to create a map using vectors.

(def^:dynamic grid-dim (atom '(0 0)))

(defn build-grid [grid-len]
  (let [x-range (range 0 grid-len) y-range (range 0 grid-len)]
      (fn [out-map x]
        (conj out-map
            (fn [map-entry y]
              (conj map-entry [x y {:rover-name 0}]))

Without the first rest, this would be a triple vector. There has to be a better way.


Filed under Clojure

2 responses to “Creating Matrices In Clojure

  1. Several better ways, but I’d use for comprehensions for anything like this:

    (defn g [grid-len]
    (for [x (range 1 grid-len)]
    (for [y (range grid-len)]
    [x y {:rover-name 0}]))))

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s