focus v0.2.0 Prism

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

This includes lists and 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 focuses on an index in a list

Define a prism to focus on a part of a data structure

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})

iex> error |> Focus.set({:error, 5}, “Banana”)

iex> error |> Focus.view({:ok, :oops}) {:error, {:prism, :bad_path}}

idx(num)
idx(number) :: Prism.t

A prism that focuses on an index in a list.

Examples

iex> first_elem = Prism.idx(0)
iex> first_elem |> Focus.view([1,2,3,4,5])
{:ok, 1}

iex> bad_index = Prism.idx(10)
iex> bad_index |> Focus.view([1,2,3])
{:error, {:prism, :bad_path}}
make_prism(path)
make_prism(any) :: Prism.t

Define a prism to focus on a part of a data structure.

Examples

iex> fst = Prism.make_prism(0)
iex> states = [:maryland, :texas, :illinois]
iex> fst.get.(states)
:maryland
iex> fst.put.(states).(:california)
[:california, :texas, :illinois]
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}}