View Source Euclid.Sugar (Euclid v0.4.0)

Some common syntactic sugar functions.

These functions are intended to be used by importing the functions or the whole module:

import Euclid.Sugar, only: [noreply: 1]

def handle_event("foo", _params, socket) do
  socket |> assign(foo: "bar") |> noreply()
end

Link to this section Summary

Functions

Wraps a term in an :error tuple. Useful in pipelines.

Unwraps an :error tuple, raising if the term is not an :error tuple

Wraps a term in a :noreply tuple

Wraps a term in an :ok tuple

Unwraps an :ok tuple, raising if the term is not an :ok tuple

Accepts two arguments and returns the second. Useful at the end of the pipeline when you want to return a different value than the last result of the pipeline, such as when the pipeline has side effects and you want to return a different value and you feel the code will be easier to read if everything is in a pipeline.

Link to this section Functions

Specs

error(term()) :: {:error, term()}

Wraps a term in an :error tuple. Useful in pipelines.

Examples

iex> %{} |> Map.put(:count, "unknown") |> Euclid.Sugar.error()
{:error, %{count: "unknown"}}

Specs

error!({:error, term()}) :: term()

Unwraps an :error tuple, raising if the term is not an :error tuple

Examples

iex> {:error, 1} |> Euclid.Sugar.error!()
1

Specs

noreply(term()) :: {:noreply, term()}

Wraps a term in a :noreply tuple

Examples

iex> %{} |> Map.put(:count, 0) |> Euclid.Sugar.noreply()
{:noreply, %{count: 0}}

Specs

ok(term()) :: {:ok, term()}

Wraps a term in an :ok tuple

Examples

iex> %{} |> Map.put(:count, 10) |> Euclid.Sugar.ok()
{:ok, %{count: 10}}

Specs

ok!({:ok, term()}) :: term()

Unwraps an :ok tuple, raising if the term is not an :ok tuple

Examples

iex> {:ok, 1} |> Euclid.Sugar.ok!()
1
Link to this function

returning(first, second)

View Source

Specs

returning(any(), any()) :: any()

Accepts two arguments and returns the second. Useful at the end of the pipeline when you want to return a different value than the last result of the pipeline, such as when the pipeline has side effects and you want to return a different value and you feel the code will be easier to read if everything is in a pipeline.

Examples

iex> %{} |> Map.put(:count, 20) |> Euclid.Sugar.returning(:count_updated)
:count_updated