Created
January 28, 2014 16:36
-
-
Save cfeduke/8671244 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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