Road to Elm - Pipes, Chaining and Nesting

If you're familiar with Linux, you have certainly encountered pipes:

find / -name somename | grep ...

they take the result from one command and pass it to the next function, allowing you to chain multiple command together.

Road to Elm - Toc

They're helpful and convenient, and take advantage of the compositionality that the language encourages. And you can have them in many FP languages (clojure, F#, Elm, ..)

In Elm:

MarioModel
  |> somefunction somearg
  |> someother function
  |> gravity

-- returns the MarioModel, which has gone through all those functions

As you can see it's quite easy to read, to format, and you can see at a glance what that MarioModel goes through.

Alternative syntaxes:

  • nesting
(grav (jump (someother (somefunction MarioModel))))

I'm a LISPer, so I love that. But I can see why people prefer pipes.

  • temp vars
var marioModel = { x: 5, y: 8, ... }
var tempMario = somefunction(marioModel);
var finalMario = gravity(tempMario);

would work in the same way, but it doesn't really exist as a standalone concept.

  • chainable

The most similar concept, is probably chainable functions, as commonly used in jQuery:

$("#mariomodel").somefunction(2000).gravity(10);

Directions

According to what looks natural to you you might prefer

  • |> function application forwards, as we saw above, or
  • <| function application backwards, which, for example, looks like this:
leftAligned <| monospace <| fromString "some text"