Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
; 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