On Wed, Sep 14, 2011 at 8:58 AM, Leonardo Borges <leonardoborges...@gmail.com> wrote: > Hi Guys, > I'm pretty new to clojure and to the list as well - this being my 1st > message - so hello everyone :) > I'm going through the book The Joy of Clojure which, pardon the pun, > I'm enJOYing a lot and stumbled upon this function to find neighbors > of a location in a 2D matrix:
Glad you like the book [careful avoidance of pun intended]! > (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)))) > > This syntax made me scratch my head since I believe it was the first > time I saw it. However, upon closer analysis it seems it could be > rewritten like this, yielding the same result: > > (defn neighbors-1 [size yx] > (let [deltas [[-1 0] [1 0] [0 -1] [0 1]]] > (filter (fn [new-yx] > (every? #(< -1 % size) new-yx)) > (map #(map + yx %) deltas)))) > > The second version feels a lot easier on my eyes. Am I missing > something or they are really equivalent? And if so, why is the first > syntax supported or better yet, when is it best to use it? The main difference is that the first version allows you to pass in your own deltas. This could be useful if you wanted to include diagonals as neighbors. We don't think we take advantage of the flexibility anywhere in the book, so perhaps your version would indeed be better. --Chouser -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en