Hello,


I'm trying to find an elegant way of indexing a two-dimensional sequence 
with the coordinates as keys. 

The input is something like.

   (

"#####"

"#...O"

"#.#.#"

"I.#.#"

"#...#"

"#####")


The midje test below indicates what kind of behaviour I'm looking for. I 
can't come up with a solution that is elegant. I'm sure there are better 
ways to do this, especially in such a sequence oriented language as clojure. 
Would someone please give me a hand here?


(defn- index-maze [str-maze] 

  (let [width (count (first str-maze))

        symbols (flatten (map seq str-maze))

        total-size (count symbols)] 

    (apply conj 

           (for [position (range total-size) 

                 :let [row    (quot position width) 

                       column (mod position width)

                       sym    (nth symbols position)]] 

             {[row column] sym}))))


(fact

  "makes a map with the coordinates as keys symbols as values"

  (index-maze '("#I#" 

                "#O#")) => (in-any-order {

                                  [0 0] \# 

                                  [0 1] \I

                                  [0 2] \#

                                  [1 0] \#

                                  [1 1] \O

                                  [1 2] \#}))


Regards

Johan Martinsson

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to