Order of Operations in comp

I’m digging back into Transducers in Clojure. This time, I think I might actually understand them.

One thing you have to get used to is replacing your reliance on the threading macros -> and ->> with the (comp) function.

TL;DR — (comp) runs in the same order as threading.

(sequence 
  (comp
    (map #(str % "a"))
    (map #(str % "b")))
  ["0", "1", "2"])

=> ("0ab", "1ab", "2ab")

The same applies when you (comp) a (comp); the order follows.

(let [p1 (comp (map #(str % "a"))  
               (map #(str % "b")))
      p2 (comp (map #(str % "y"))
               (map #(str % "z")))]
  (sequence (comp p1 p2) 
            ["0" "1" "2"]))

=> ("0abyz", "1abyz", "2abyz")

Leave a Reply