Search code examples
purescript

What does the triple less-than sign (`<<<`) do in PureScript?


I've seen this code in a PureScript program, what does <<< do?

pinkieLogic :: (Tuple Boolean GameObject) -> GameObject -> GameObject
pinkieLogic (Tuple jumpPressed hater) p =
  hated hater p
  (solidGround
   <<< gravity
   <<< velocity
   <<< jump jumpPressed
   <<< clearSound)

Solution

  • <<< is the right-to-left composition operator. It's equivalent to . in Haskell. It works like this:

    (f <<< g) x = f (g x)
    

    That is, if you have two functions1 and you put <<< between then, you'll get a new function that calls the first function with the result of calling the second function.

    So, that code could be rewritten as follows:

    pinkieLogic :: (Tuple Boolean GameObject) -> GameObject -> GameObject
    pinkieLogic (Tuple jumpPressed hater) p =
      hated hater p
      (\x -> solidGround (gravity (velocity (jump jumpPressed (clearSound x)))))
    

    [1] Unlike Haskell's . operator, <<< in PureScript also works on categories or semigroupoids.