The Amazing Pipe Operator: |>

I’ve saved the best for last, at least when it comes to functions.

You’ve all seen code like this:

 people = DB.find_customers
 orders = Orders.for_customers(people)
 tax = sales_tax(orders, 2016)
 filing = prepare_filing(tax)

Bread-and-butter programming. We did it because the alternative was to write

 filing = prepare_filing(sales_tax(Orders.for_customers(DB.find_customers), 2016))

and that’s the kind of code that you use to get kids to eat their vegetables. Not only is it hard to read, but you have to read it inside out if you want to see the order in which things get done.

Elixir has a better way of writing it.

 filing = DB.find_customers
  |> Orders.for_customers
  |> sales_tax(2016)
  |> prepare_filing

The ...

Get Programming Elixir 1.2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.