Reputation: 4171
I want to produce a seq that I can later do a (map) over. It should look like this:
((0 0) (0 1) (0 2) (0 3) ... (7 7))
The piece of code I have to do it right now seems very, very ugly to produce such a simple result. I need some help getting this straight.
(loop [y 0 x 0 args (list)]
(if (and (= y 7) (= x 7))
(reverse (conj args (list y x)))
(if (= x 7)
(recur (+ y 1) 0 (conj args (list y x)))
(recur y (+ x 1) (conj args (list y x))))))
Upvotes: 2
Views: 173
Reputation: 2868
There's a library function that does that:
(use '[clojure.contrib.combinatorics])
(cartesian-product (range 0 8) (range 0 8))
Upvotes: 5
Reputation: 3408
(let [my-range (range 0 8)]
(for [i my-range
j my-range]
(list i j)))
=> ((0 0) (0 1) (0 2) (0 3) (0 4) (0 5) (0 6) (0 7)
(1 0) (1 1) (1 2) (1 3) (1 4) (1 5) (1 6) (1 7)
...
(7 0) (7 1) (7 2) (7 3) (7 4) (7 5) (7 6) (7 7))
for is like doseq, but it collects results:
(for [i [1 2 3]] i) => (1 2 3)
(doseq [i [1 2 3]] (print i)) => nil
Upvotes: 13