/elixir-pattern_tap

Macro for tapping into a pattern match while using the pipe operator

Primary LanguageElixir

PatternTap

The pipe operator |> is an awesome feature of Elixir.

But when your result cannot be directly input into the next function, you have to stop, pattern match out the value you want and start piping again!

It is a common pattern to return data like {:ok, result} or {:error, reason}. When you want to handle both cases, something like elixir-pipes may be a better use case for you. But otherwise, for simple destructuring of data and returning it in one line (or to just let it fail) you can use PatternTap!

Not fun way

defmodule Foo do
  def get_stuff(input) do
    {:ok, intermediate_result} = input
      |> Enum.map(&(to_string(&1)))
      |> Foo.HardWorker.work
    {:ok, result} = intermediate_result
      |> Enum.map(&(Foo.IntermediateResult.handle(&1)))
    result
  end
end

Anytime where the object you want requires pattern matching but you want to either return on one line or continue piping, you can use PatternTap!

def my_function do
  {:ok, result} = something |> something_else
  result
end

Pattern Tap

Heres the above example using PatternTap

defmodule Foo do
  def get_stuff(input) do
    input
      |> Enum.map(&(to_string(&1)))
      |> Foo.HardWorker.work
      |> tap({:ok, r1} ~> r1) # tap({:ok, r1}, r1) is also a supported format
      |> Enum.map(&(Foo.IntermediateResult.handle(&1)))
      |> tap({:ok, r2} ~> r2) # tap({:ok, r2}, r2) is also a supported format
  end
end

And the second example

# tap({:ok, result}, result) also supported
def my_function do
  something |> something_else |> tap({:ok, result} ~> result)
end

Variable Leakage

PatternTap makes use of case in order to prevent leaking the variables you create. So after using tap, you won't have access to the patterns you create. This means if you bind more than one variable in your pattern, you won't have access to it.

Take the following example:

my_data = {:data1, :data2} |> tap({d1, d2} ~> d1)
d2 # => ** (CompileError) ...: function d2/0 undefined

Instead you can use destruct to destructure the data you want. This does the same thing but with the side effect of keeping the binding you created in your patterns.

{:data1, :data2} |> destruct({d1, d2} ~> d1) |> some_func(d2)

Unmatched results

Tap

Because tap/3 uses case you will get a CaseClauseError with the data which did not match in the error report.

{:error, "reason"} |> tap({:ok, result} ~> result)
# ** (CaseClauseError) no case clause matching: {:error, "reason"}

Destruct

Since destruct/3 uses = you will instead get a MatchError with the data which did not match in the error report.

{:error, "reason"} |> destruct({:ok, result} ~> result)
# ** (MatchError) no match of right hand side value: {:error, "reason"}