Pathex.Lenses (Pathex v1.2.0) View Source
Module with collection of prebuilt paths
Link to this section Summary
Functions
Path function which works with all possible keys it can find
It takes all keys and than applies inner function (or concated path)
If any application fails, this lens returns :error
Path function which works with any possible key it can find It takes any key and than applies inner function (or concated path)
Path function for tuple with specified first element
Path function which works like unix path ./
Works with every existing value (not only Enumerable)
Path function which applies inner function (or concated path-closure) to every value it can apply it to
Link to this section Functions
Specs
all() :: Pathex.t()
Path function which works with all possible keys it can find
It takes all keys and than applies inner function (or concated path)
If any application fails, this lens returns :error
Example:
iex> require Pathex; import Pathex
iex> alll = Pathex.Lenses.all()
iex> [%{x: 1}, [x: 2]] = Pathex.over!([%{x: 0}, [x: 1]], alll ~> path(:x), fn x -> x + 1 end)
iex> [1, 2, 3] = Pathex.view!(%{x: 1, y: 2, z: 3}, alll) |> Enum.sort()
iex> {:ok, [x: 2, y: 2]} = Pathex.set([x: 1, y: 0], alll, 2)
Specs
any() :: Pathex.t()
Path function which works with any possible key it can find It takes any key and than applies inner function (or concated path)
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
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 ./
Works with every existing value (not only Enumerable)
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
Specs
star() :: Pathex.t()
Path function which applies inner function (or concated path-closure) to every value it can apply it to
Example:
iex> require Pathex; import Pathex
iex> starl = Pathex.Lenses.star()
iex> [1, 2] = Pathex.view!(%{x: [1], y: [2], z: 3}, starl ~> path(0)) |> Enum.sort()
iex> %{x: %{y: 1}, z: [3]} = Pathex.set!(%{x: %{y: 0}, z: [3]}, starl ~> path(:y, :map), 1)
iex> {:ok, [1, 2, 3]} = Pathex.view([x: 1, y: 2, z: 3], starl)
Note:
Force update works the same way asall
lens
And update leaves unusable data unchanged