focus v0.2.2 Prism

Prisms are like lenses, but used when the view focused on may not exist.

This includes sum types; although not backed by an explicit Maybe type, the [{:ok, any} | {:error}] convention is explicitly supported as a value that a prism can focus on.

Summary

Functions

A prism that matches an {:error, } tuple. Note that on a successful match, view/set/over will return {:ok, }

A prism that matches an {:ok, _} tuple

Types

t()
t() :: %Prism{get: (any -> any), put: ((any -> any) -> any)}

Functions

error()
error() :: Prism.t

A prism that matches an {:error, } tuple. Note that on a successful match, view/set/over will return {:ok, }

Examples

iex> error = Prism.error
iex> error |> Focus.view({:error, 5})
{:ok, 5}
iex> error |> Focus.set({:error, 5}, "Banana")
{:ok, "Banana"}
iex> error |> Focus.view({:ok, :oops})
{:error, {:prism, :bad_path}}
ok()
ok() :: Prism.t

A prism that matches an {:ok, _} tuple.

Examples

iex> ok = Prism.ok
iex> ok |> Focus.view({:ok, 5})
{:ok, 5}
iex> ok |> Focus.set({:ok, 5}, "Banana")
{:ok, "Banana"}
iex> ok |> Focus.view({:error, :oops})
{:error, {:prism, :bad_path}}