-
Notifications
You must be signed in to change notification settings - Fork 5
Create a record for maze #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
YES. We are doing this already in a lot of places. I'd like to see this too. |
So... I tried to see how this would look in a first pass by just sticking everything into a My first thought for the (defrecord Maze [size paths current-path visited walls doors update-channel
search-algorithm possible-paths-fn finished-fn])
(defn make-maze [{:keys [size paths current-path visited walls doors
update-channel search-algorithm possible-paths-fn
finished-fn]
:or {search-algorithm depth-first
possible-paths-fn shuffled-possible-paths
update-channel (chan (dropping-buffer 1))
paths [[[0 0]]]
visited #{}
doors #{}}}]
(Maze. size paths current-path visited walls doors update-channel
search-algorithm possible-paths-fn finished-fn)) I'm not a fan of this though because we duplicate our parameter list 4 times. It's not very pretty at all. I tried to clean this up by merging any options we provide with a set of defaults, but this looks dumb because (defrecord Maze [size paths current-path visited walls doors update-channel
search-algorithm possible-paths-fn finished-fn])
(defn make-maze [options]
(let [defaults {:search-algorithm depth-first
:possible-paths-fn shuffled-possible-paths
:update-channel (chan (dropping-buffer 1))
:paths [[[0 0]]]
:visited #{}
:doors #{}}]
(merge (Maze. nil nil nil nil nil nil nil nil nil nil)
(merge defaults options)))) Because JavaScript is hella janky and doesn't enforce arity, we can acutally get away with not passing all the nils into (defrecord Maze [size paths current-path visited walls doors update-channel
search-algorithm possible-paths-fn finished-fn])
(defn make-maze [options]
(let [defaults {:search-algorithm depth-first
:possible-paths-fn shuffled-possible-paths
:update-channel (chan (dropping-buffer 1))
:paths [[[0 0]]]
:visited #{}
:doors #{}}]
(merge (Maze.) (merge defaults options)))) I still don't like this solution though because we get a warning when compiling and it flat out doesn't work in Clojure.
The only decent solution I've been able to find is exactly like the first example we have above, but creating a (defrecord+ Maze
[size nil
paths [[[0 0]]]
current-path nil
visited #{}
walls nil
doors #{}
update-channel (chan (dropping-buffer 1))
search-algorithm depth-first
possible-paths-fn shuffled-possible-paths
finished-fn nil]) What are your thoughts on this? Is there another convention for class defaults that you've seen somewhere? The macro seems to make the most sense, but I feel like I might be missing something here. |
Might clean up the code a bit to have an explicit record. Shouldn't require many changes, since records can do almost everything maps can, but are significantly more performant, and help document what your data looks like.
If we go this route, I'd like to experiment passing the entire maze structure to all fns, with each fn destructuring out the pieces it needs.
The text was updated successfully, but these errors were encountered: