Skip to content

Instantly share code, notes, and snippets.

@cfeduke
Created January 28, 2014 16:36
Show Gist options
  • Save cfeduke/8671244 to your computer and use it in GitHub Desktop.
Save cfeduke/8671244 to your computer and use it in GitHub Desktop.
; Joy of Clojure pp85-86
(defn neighbors
([size yx] (neighbors [[-1 0] [1 0] [0 -1] [0 1]] size yx))
([deltas size yx]
(filter (fn [new-yx]
(every? #(< -1 % size) new-yx))
(map #(map + yx %) deltas))))
; usage
(def matrix
[[1 2 3]
[4 5 6]
[7 8 9]])
(map #(get-in matrix %) (neighbors 3 [0 0]))
;=> (4 2)
@cfeduke
Copy link
Author

cfeduke commented Jan 28, 2014

The key to understanding this is that it is method overloading by the arity of the arguments.

SO answer explaining arity overloading
Dave Newton: so you're either passing in a size and yx, or the delta and the same two things

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment