Arc Forumnew | comments | leaders | submitlogin
1 point by almkglor 5723 days ago | link | parent

Assuming you want to use showpos (which is arguably slower since you have to traverse the list each time):

  (mac w/collect body
    `(accum collect ,@body))

  (def observe (x y r)
    (w/collect:for i (- x r) (+ x r)
      (for j (- y r) (+ y r)
        (awhen (showpos i j)
          (collect it)))))
If you want efficiency and still want to use list structures, you might want to skip 'showpos.


1 point by bOR_ 5723 days ago | link

Good call. Not necessarily want to use showpos.. its main advantage right now is that it nicely returns nil when I try to read any out-of-bound position.

I guess without showpos I could grab the appropriate rows from world and discard the first X and last Y positions.

-----

1 point by bOR_ 5722 days ago | link

There we go. That looks like a solution I was looking for :). and cut nicely takes care of out-of-bounds too.

  (map (fn (_) (cut _ (- row 1) (+ col 2))) (cut world (- row 1) (+ col 2)))

-----

1 point by bOR_ 5722 days ago | link

ugh. just don't mind the nonsensical placing of row and col, it should be row row, col col

-----

1 point by almkglor 5722 days ago | link

  (map [cut _ (- row r) (+ row r)] (cut world (- col r) (+ col r)))

-----

1 point by bOR_ 5722 days ago | link

From here on I adapted the function to the one I really needed (do something if one of the 8 neighbours of posx, posy is X). Because of that requirement it seemed easier to just gather the row - 1 and row + 1 and the row, col - 1 and row, col + 1, and join those in a list. This is what it ended up as:

   (if (find X (flat:map [join (errsafe (world _))] (list (- row 1) (+ row 1) (- col 1) (+ col 1))))
    ; do stuff
    )
Thanks for the help!

-----

1 point by almkglor 5722 days ago | link

  (flat:map [join (errsafe (world _))] (list (- row 1) (+ row 1) (- col 1) (+ col 1)))
This bit doesn't seem correct... world is addressed by row right? Why are you also addressing by column?

-----

1 point by bOR_ 5721 days ago | link

yeah. realized this night (dreaming is a wonderful thing) that I'm now making a mess out of it, rather than just looking at neighbours. bug! ;).

But as far as I'm concerned, I've had enough help already. Should be able to take it from here.

-----