Reputation: 33
I'm still learning Clojure and can't seem to find a simple answer for this. I've seen similar questions answered with complex code specific for the OP's issue so please let me know the most accurate or acceptable version of the following:
int[][] arrayTest = new int[width][height];
...
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int a = arrayTest[x][y];
if (a < 100) {
arrayTest[x][y] = 0;
}
}
}
Upvotes: 3
Views: 1895
Reputation: 876
Represented as a sequence of sequences...
user=> (doc repeat)
-------------------------
clojure.core/repeat
([x] [n x])
Returns a lazy (infinite!, or length n if supplied) sequence of xs.
nil
user=> (def width 8)
#'user/width
user=> (def height 6)
#'user/height
user=> (repeat width 0)
(0 0 0 0 0 0 0 0)
user=> (pprint (repeat height (repeat width 0)))
((0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0))
nil
Upvotes: 0
Reputation: 5395
The literal translation is straightforward:
(def array-test
(make-array Integer/TYPE width height))
(doseq [x (range width)
y (range height)]
(when (< (aget array-test x y) 100)
(aset-int array-test x y 0)))
Note, however, that arrays are not commonly used in Clojure. Unless you want to do fast computations or work with existing Java code, you shouldn't normally be creating arrays and other mutable data structures. Most likely, what you want to implement can be done with Clojure's persistent collections instead.
Upvotes: 7