Thursday, September 27, 2012

Clojure: Refactoring From Thread Last (->>) To Thread First (->)

I use ->> (thread-last) and -> (thread-first) very often. When I'm transforming data I find it easy to break things down mentally by taking small, specific steps, and I find that -> & ->> allow me to easily express my steps.

Let's begin with a (very contrived) example. Let's assume we have user data and we need a list of all users in "new york", grouped by their employer, and iff their employer is "drw.com" then we only want their name - otherwise we want all of the user's data. In terms of the input and the desired output, below is what we have and what we're looking for.


A solution that uses ->> can be found below.


The above example is very likely the first solution I would create. I go about solving the problem step by step, and if the first step takes my collection as the last argument then I will often begin by using ->>. However, after the solution is functional I will almost always refactor to -> if any of my "steps" do not take the result of the previous step as the last argument. I strongly dislike the above solution - using an anonymous function to make update-in usable with a thread-last feels wrong and is harder for me to parse (when compared with the alternatives found below).

The above solution could be refactored to the following solution


This solution is dry, but it also groups two of my three steps together, while leaving the other step at another level. I expect many people to prefer this solution, but it's not the one that I like the best.

The following solution is how I like to refactor from ->> to ->


My preferred solution has an "extra" thread-last, but it allows me to keep everything on the same level. By keeping everything on the same level, I'm able to easily look at the code and reason about what it's doing. I know that each step is an isolated transformation and I feel freed from keeping a mental stack of what's going on in the other steps.

7 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. I have really appreciated this and the previous blog post.

    In the spirit of your previous blog post I had a go at re-imagining the example with clojure.set: https://gist.github.com/3799887

    ReplyDelete
  3. thanks alex. I added another comment that shows how things could be done in one pass with clojure.set.

    Cheers, Jay

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Cool stuff! Posted a comment in the gist :)

    ReplyDelete
  6. Anonymous6:08 PM

    Interesting post. I tried the challenge myself but without using -> or ->> (which I also like, but here I prefered to work without them):

    => (let [people [jay john mike chris]
    => new-yorkers (filter #(= "new york" (:current-city %)) people)
    => by-employer (group-by :employer new-yorkers)]
    => (assoc by-employer "drw.com" (map :name (get by-employer "drw.com"))))
    {"drw.com" ("jay fields" "john dydo"), "thoughtworks.com" [{:name "chris george", :current-city "new york", :employer "thoughtworks.
    com"}]}

    ReplyDelete
  7. In situations where functions in a threading form take arguments in an irregular order, the following macro from "Clojure in Action" book is useful:

    (defmacro thread-it [& [first-expr & rest-exprs]]
    (if (empty? rest-exprs)
    first-expr
    `(let [~'it ~first-expr]
    (thread-it ~@rest-exprs))))

    With this macro the example looks like this:

    (thread-it
    [jay john mike chris]
    (filter (comp (partial = "new york") :current-city) it)
    (group-by :employer it)
    (update-in it ["drw.com"] (partial map :name)))

    We can also name it, for example, >> (https://gist.github.com/4026073).

    ReplyDelete

Note: Only a member of this blog may post a comment.