ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - mgwidmann/elixir-pattern_tap: Macro for tapping into a pattern match while using the pipe operator
Macro for tapping into a pattern match while using the pipe operator - mgwidmann/elixir-pattern_tap
Visit Site

GitHub - mgwidmann/elixir-pattern_tap: Macro for tapping into a pattern match while using the pipe operator

GitHub - mgwidmann/elixir-pattern_tap: Macro for tapping into a pattern match while using the pipe operator

PatternTap

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

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)

To simply save a partial result for later use, consider using leak/2:

iex> [:data1, :data2] |> Enum.reverse |> leak(reversed) |> hd
:data2
iex> reversed
[:data2, :data1]

Note that |> leak(variable_name) is equivalent to |> destruct(variable_name ~> variable_name).

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 and leak/2 use = 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"}

Leak

leak(data, variable) expands to variable = data, so in a simple use case, leak can never fail, though it may override an existing variable:

iex> old_var = 5
iex> [1, 2] |> leak(old_var) |> length
2
iex> old_var
[1, 2]

Because leak(data, variable) expands to variable = data, we can do all of our favorite Elixir pattern-matching tricks here, e.g.:

iex> %{a: 1, b: 2} |> leak(%{b: b})
%{a: 1, b: 2}
iex> b
2

This flexibility allows leak to fail just like destruct:

iex> %{a: 1, b: 2} |> leak(%{c: c})
** (MatchError) no match of right hand side value: %{a: 1, b: 2}

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory