3

The documentation for comp states that it starts with the rightmost function, applies the arguments to it, then gives the result to the next function and so on. Hence here the numbers are added first then str is applied to the integer 16:

((comp str +) 8 8) ;;=> "16"

This code is from example 7 in the core.async webinar:

mouse (events->chan js/window EventType.MOUSEMOVE
       (chan 1 
       (comp (map mouse-loc->vec) 
             (filter (fn [[_ y]] (zero? (mod y 5)))))))

Here a stream of mouse events are the arguments. They are first converted into a pair (tuple 2 vector) and then these pairs are filtered. The map function (which happens to be a transducer) needs to receive the mouse event before the filter function, and obviously that is what is actually happening because this code works. So why is the first operation (the map function) not the rightmost function argument to comp?

Answer In the video at 25:30 Rich Hickey says "transducers ruin comp or something like that" - after that he explains the issue. Thanks @nblumoe

Chris Murphy
  • 6,411
  • 1
  • 24
  • 42

1 Answers1

3

Here is a snippet from the transducer documentation, explaining the behavior:

Composition of the transformer runs right-to-left but builds a transformation stack that is applied left-to-right

http://clojure.org/transducers (see "Defining Transformations with transducers")

Nils Blum-Oeste
  • 5,608
  • 4
  • 25
  • 26