如何在2D向量中更新结构值? Clojure

How to update structure value within a 2D vector? Clojure

这是在CLojure中,首先是我的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
;cell structure
(defstruct cell :x :y :highland :lowland :obstacle :ammo :tank)

;demension
(def dim 0)

(defn setdim [num]
  (def dim num))

;create world
(defn creatworld []
  (apply vector
         (map (fn [_]
                (apply vector (map (fn [_] (struct cell))
                                   (range dim))))
              (range dim))))

;initiate coordinate for structure in vector of vector
;this is not working
(defn inicoor [world]
  (map
   #(assoc % :x i :y j)
   world))

(defn inicoor [world]
  (dorun (for [i (range 0 dim)]
           (dorun (for [j (range 0 dim)]
                    (map  
                    # (assoc (nth (nth world i) j) :x i :y j)))))))

因此,我正在做的是尝试使用结构的2D向量创建2D世界。 创建世界后,我希望像在上一个函数中尝试的那样将x y坐标初始化为实际坐标。 但是,由于clojure是不可变的变量,因此不会更改值...而且它也不会返回新数据的二维向量...
然后我尝试使用地图...但是我真的是clojure的新手...几次尝试后都无法使用...

谁能告诉我该怎么做? 非常感谢...

加:
目标结构如下:

1
2
3
4
5
6
[  00 10 20 30 40 50   ]  (this is the first vector)
   01 11 21 31 41 51
   02 12 22 32 42 52
   03 13 23 33 43 53
   04 14 24 34 44 54
   05 15 25 35 45 55

这就是为什么我首先使用一个嵌套循环的原因...在Java中最常用的方法...


使用法线贴图,您可以说:

1
(for [y (range 8) x (range 8)] {:x x :y y})

在一个大列表中获得具有其坐标的单元格

更新它们(假设您有一个fn update-cell [cell]:

1
 (map update-cell cells)

或者,如果您有一些数据可用来更新它们:

1
(map update-cell cells (repeat data))
  • 该数据可能类似于{:t the-time:dt ms-since-; last-update:etc other-stuff}

如果要将它们放在二维网格中,则可以执行以下操作:

1
(partition  8 (for [y (range 8) x (range 8)] {:x x :y y}))

或者说您有一个(defn make-cell [x y] {:x x :y y}),则可以使它们:

1
(map (fn [y] (map (fn [x] (make-cell x y)) (range width))) (range height))

然后更新它们:

1
(map (partial map update-cell) cells)