Hey cycl...@speakeasy.net,

Jay's excellent main point is really about the use of anonymous functions-
that in many cases it's an anti-pattern.

Having a name for every function means that those functions become testable
and that code consuming those functions stays readable.

Another point is that core has a lot of functionality and especially when
one is new to the functional mindset, it's easy to miss something that
makes your job easy.

There will be many ways of expressing a particular operation, and it's
worth trying a few different ways to get to the semantics that make most
sense.

Here's one suggestion- turn the anonymous function into a named function
and use partial to compose the rule you need:

(defn quad-piece?
  [row col piece]
  (and (= row (:quad-row piece))
         (= col (:quad-col piece))))

(defn get-quad-pieces
  [galaxy [row col]]
  (filter (partial quad-piece? row col) galaxy))

Then maybe it looks like get-quad-pieces isn't doing very much and maybe
there are some filter cases to capture instead:

(def zeros (partial quad-piece? 0 0))

(filter zeros test-galaxy)

Anyway, hope that's helpful.

Jonah



On Wed, Jan 27, 2016 at 8:19 PM, <cycl...@speakeasy.net> wrote:

> (def test-galaxy [{:quad-col 0, :quad-row 0, :sec-row 4, :sec-col 4, :type
> :E}
>                    {:quad-col 0, :quad-row 0, :sec-row 4, :sec-col 3,
> :type :base}
>                    {:quad-col 3, :quad-row 5, :sec-row 7, :sec-col 5,
> :type :star}
>                    {:quad-col 1, :quad-row 3, :sec-row 0, :sec-col 5,
> :type :klingon}])
>
> (defn get-quad-pieces
>   [galaxy [quad-row quad-col]]
>     (let [quad-pieces (filter (fn [elem] (and (= (:quad-row elem) quad-row)
>                                               (= (:quad-col elem)
> quad-col))) galaxy)]
>     quad-pieces))
>
> (get-quad-pieces test-galaxy [0 0])
> ; ({:quad-col 0, :quad-row 0, :sec-row 4, :sec-col 4, :type :E} {:quad-col
> 0, :quad-row 0, :sec-row 4, :sec-col 3, :type :base})
>
>
> Jay Fields makes an excellent argument for using core functions, instead
> of anonyous functions, wherever possible
> http://blog.jayfields.com/2012/10/clojure-avoiding-anonymous-functions.html
> In his example, he was grouping elements, not filtering by multiple
> values, which is what I'm trying to do.
>
> I'm trying to refactor one particular piece of code, and have no idea
> what, if any, core functions, would work.
>
> I want to filter a list of hashes, based on some of the values - (code
> topside)
>
> I have come up with a few variations of the code, with reduce, etc - but
> they all use an anonyous function.  The only options I can figure, using
> filter or reduce, require a variable to represent each list element, which
> means an anon-function with a parameter.
>
> Thanks.
>
> Okay, I now have two questions - why does the code-highlighter insist on
> putting the code at the top, instead of in the middle, where I had the
> cursor? :)
>
>
> --
> 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
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to