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


this in clojure, first code:

;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 structure in vector of vector ;this not working (defn inicoor [world]   (map    #(assoc % :x :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 :y j))))))) 

so, i'm doing try create 2d world using 2d vector of structure. after create world, wish initiate x y coordinate actual coordinate, tried in last function. however, since clojure variable immutable, it's not going change value... , not return 2d vector of new data... tried use map... i'm new clojure... didn't work after few try...

can tell me how it? lot...

add: target structure this:

[  00 10 20 30 40 50   ]  (this 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 

that's why used nest loop @ first... common way in java...

with normal maps say:

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

to cells coordinates in 1 big list

to update them (assuming have fn update-cell [cell]:

 (map update-cell cells) 

or if had data update them with:

(map update-cell cells (repeat data)) 
  • that data stuff {:t the-time :dt ms-since-;last-update :etc other-stuff}

if want them in 2d grid, do:

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

or, had (defn make-cell [x y] {:x x :y y}), make them:

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

then update them:

(map (partial map update-cell) cells) 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -