Clojure中的简单2D数组

use*_*182 3 clojure

我还在学习Clojure,似乎无法找到一个简单的答案.我已经看到类似的问题用针对OP问题的复杂代码回答,所以请让我知道以下最准确或可接受的版本:

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;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*eph 6

字面翻译很简单:

(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)))
Run Code Online (Sandbox Code Playgroud)

但请注意,数组在Clojure中并不常用.除非您想进行快速计算或使用现有Java代码,否则通常不应该创建数组和其他可变数据结构.最有可能的是,您想要实现的内容可以通过Clojure的持久集合来完成.