Magik.Maybe.pipe

You're seeing just the function pipe, go back to Magik.Maybe module for more information.

Specs

pipe({:ok, any()} | any(), function()) :: any()

This function check first argument, if it is tuple of {:ok, data} then invoke the function with data of the tuple. Otherwise return the first argument.

Example

changeset = User.changeset(%User{}, params)
Repo.insert(changeset)
|> Maybe.pipe(fn user ->
  # do something with user
end)

In case you are using with, don't care about this function

Link to this function

pipe(data, guard_func, func)

View Source

Specs

pipe(any(), condition :: any(), function()) :: any()

This helper is used in pipe, to invoke function if condition is true, otherwise passing data to nex function

Example

a = 10
b = 12

a
|> Kernel.*(2)
|> Maybe.pipe( b != 0, & &1/b)
|> Kernel.+(10)

The condition Could be any value:

  • if condition is true then call the function with the first argument
  • if condition is function/1 then invoke condition function. If return value is true then invoke the function with first argument
  • other value of condition don't trigger the function and just return the first argument that passed to this function

Example 2 Using function condition

a = 10
a
|> Kernel.+(3)
|> Maybe.pipe(& &1 > 0, & &1 - 5)