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
Functions
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}}
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}}
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]