Elixir tap and then macros - life-saving helpers

One of the last updates in Elixir (version 1.12, to be precise) introduced two very useful macros. They are them/2 and tap/2. I would like to introduce how useful it can be to use these expressions in your code.

Differences between macros

Both macros take exactly two arguments. The first is the data, while the second is the function performed on the data. The difference is which information is to be returned as a result.

In the case of tap/2, the result is the data given as the first argument of the call. The result of the passed function has no meaning; it is ignored.

For then/2, the result is calculated based on the function passed as the second argument.

Usage

tap/2 is excellent for logging information - log or track statements’ execution flow before producing the final output. Useful, especially inside pipelines.

1
2
3
4
5
  posts
  |> tap(&IO.inspect(&1, label: "Data")) # <#--- HERE
  |> preload_authors()
  |> tap(&IO.inspect(&1, label: "Preloaded")) # <#--- HERE
  |> send_notifications()

then/2 will come in handy when we perform an action based on the data.

1
2
3
  balance
  |> Bank.calculate_fees()
  |> then(&Bank.calculate_credit_worthiness(:credit, &1, balance)) # <#--- HERE

Both macros are usable directly by name as they are available in the Kernel module.

Get new posts and extra comments

You'll receive every new post with extra unpublished comments available only to the subscribers!

I won't send you spam. Unsubscribe at any time.