Clojure: for for n Dimensions

Viewed 309

In Clojure the function for can be used for iterating nested sequences. Imagine a 3D space with a x-, y- and z-axis:

(for [x (range 10)
      y (range 5)
      z (range 2)]
    [x y z])

The code above would produce a sequence of vectors which represents all possible positions inside a cuboid. (limited to the positions which indices are natural numbers of course)

Does anybody know a good way to make this more generic? Meaning, to make it work if you have not a number of 3 but of n dimensions.

3 Answers
Related