Hacker News new | ask | show | jobs
by platz 2715 days ago
but nobody uses it because "function composition" reads the other way and tends to be preferable i.e.

  (g ∘ f)(x) = g (f(x))    // math

  (g . f) x  = g (f x)     // haskell

   g   f  x    g  f x      // see how the g f x order always matches with the default application and composition notation.
you can mess with the order and "turn the order of application "inside-out" but then you have to switch modes from left-to-right and right-to-left more often. Or you can try to write in a style that prefers right-to-left.
3 comments

I got used to this in Haskell, but I have to say that Elm's solution is pretty elegant:

  Elm:               Haskell:

  f <| g <| x   ==   f $ g  $  x    -- application

  f << g        ==   f . g          -- composition

  x |> g |> f   ==   x & g  &  f    -- reverse application

       g >> f   ==       g >>> f    -- reverse composition
It's nice to be able to change between composition and application by changing between | and < in the operator. I've actually done this quite a lot when refactoring code in Elm lately, there's something visual and intutive about it that I like. It makes it easier to mix left-to-right and right-to-left in the same code, while keeping it obvious what's going on.

I've found that certain parts, that scan/read better as imperative steps (think chains of List.map, Maybe.map, <MonadlikeModule>.andThen etc.) often benefit from a reverse application and/or composition. Sort of to simulate the look of do notation since Elm doesn't have that. Smaller functional parts are better with regular application/composition. With the directional operators, this becomes quite ergonomical.

Probaly helps that I use (and enjoy, ymmv) a font with ligatures for these symbols.

If the left to right order is preferred, Haskell has Control.Category.(>>>)

    f >>> g = g . f
Works for any instance of Category.

    g f x 
would not be the same as

    (g . f) x
you need the right associative function application operator $, ie.

    g $ f x
or simply

    g (f x)
i know that, i was just highlighting the appearance of the order of the letters without considering associativity.