Pathex.Lenses (Pathex v1.0.0) View Source

Module with collection of prebuilt paths

Link to this section Summary

Functions

Path function which works with any possible key it can find

Path function for tuple with specified first element

Path function which works like unix path ./

Link to this section Functions

Specs

any() :: Pathex.t()

Path function which works with any possible key it can find

Example:

iex> require Pathex
iex> anyl = Pathex.Lenses.any()
iex> {:ok, 1} = Pathex.view %{x: 1}, anyl
iex> {:ok, [9]} = Pathex.set  [8], anyl, 9
iex> {:ok, [x: 1, y: 2]} = Pathex.force_set [x: 0, y: 2], anyl, 1

Note that force setting value to empty map has undefined behaviour and therefore returns an error:

iex> require Pathex
iex> anyl = Pathex.Lenses.any()
iex> :error = Pathex.force_set(%{}, anyl, :well)

And note that this lens has keywords at head of list at a higher priority than non-keyword heads:

iex> require Pathex
iex> anyl = Pathex.Lenses.any()
iex> {:ok, [{:x, 1}, 2]} = Pathex.set([{:x, 0}, 2], anyl, 1)
iex> {:ok, [1, {:x, 2}]} = Pathex.set([0, {:x, 2}], anyl, 1)
iex> {:ok, [1, 2]} = Pathex.set([{"some_tuple", "here"}, 2], anyl, 1)

Specs

either(any()) :: Pathex.t()

Path function for tuple with specified first element

Example:

iex> require Pathex; import Pathex
iex> okl = Pathex.Lenses.either(:ok)
iex> 8 = view! {:ok, 8}, okl
iex> {:ok, 10} = set! {:ok, 8}, okl, 10
iex> {:ok, 123} = force_set! {:error, :x}, okl, 123

Specs

id() :: Pathex.t()

Path function which works like unix path ./

Example:

iex> require Pathex
iex> idl = Pathex.Lenses.id()
iex> {:ok, 8} = Pathex.view 8, idl
iex> {:ok, 9} = Pathex.set 8, idl, 9
iex> {:ok, 9} = Pathex.force_set 8, idl, 9