> Hello,
>
>
> I'm trying to find an elegant way of indexing a two-dimensional sequence
> with the coordinates as keys.
>
If you know width and height you can generate the pattern of coordinates instead
of calculating it:
(map vector (for [y (range height) x (range width)] y)
(cycle (range width)))
=> ([0 0] [0 1] [0 2] [1 0] [1 1] [1 2]) ; for height 2, width 3
or if you don't like 'for':
(map vector (mapcat #(repeat width %) (range height))
(cycle (range width)))
now combine with a one-dimensional input string:
(zipmap (map vector (for [y (range height) x (range width)] y)
(cycle (range width)))
"#I##O#")
=> {[1 2] \#, [1 1] \O, [1 0] \#, [0 2] \#, [0 1] \I, [0 0] \#}
HTH,
Jan
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en