Search code examples
clojure

What does ->> mean in Clojure?


I am learning Clojure, and I came across this example:

  (defn people-in-scenes [scenes]
     (->> scenes
         (map :subject)
         (interpose ", ")
         (reduce str)))

What does ->> do exactly?


Solution

  • ->> is the "thread-last" macro. It evaluates one form and passes it as the last argument into the next form.

    Your code is the equivalent of:

    (reduce str (interpose ", " (map :subject scenes)))
    

    Or, to see it a different way:

    (reduce str
                (interpose ", "
                                (map :subject scenes)))
    

    When reading clojure code, one almost has to do so from the "inside out" or from the "bottom up." The threading macros allow you to read code in what some would say is a more logical order. "Take something, first do this to it, next do that, next ...".