focus v0.2.0 Lens
Lenses combine getters and setters for keys in data structures.
Lenses should match/operate over a single value in a data structure, e.g. a key in a map/struct.
Summary
Functions
Define a lens to Focus on a part of a data structure
Automatically generate the valid lenses for the supplied map-like data structure
Get a piece of a data structure that a lens Focuses on; returns {:ok, data} | {:error, :bad_lens_path}
Types
Functions
Define a lens to Focus on a part of a data structure.
Examples
iex> person = %{name: "Homer"}
iex> name_lens = Lens.make_lens(:name)
iex> name_lens |> Focus.view(person)
"Homer"
iex> name_lens |> Focus.set(person, "Bart")
%{name: "Bart"}
make_lenses(Focus.Types.traversable) :: %{optional(atom) => Lens.t, optional(String.t) => Lens.t}
Automatically generate the valid lenses for the supplied map-like data structure.
Examples
iex> lisa = %{name: “Lisa”, pets: %{cat: “Snowball”}} iex> lisa_lenses = Lens.make_lenses(lisa) iex> lisa_lenses.name …> |> Focus.view(lisa) “Lisa” iex> pet_lenses = Lens.make_lenses(lisa.pets) iex> lisa_lenses.pets …> ~> pet_lenses.cat …> |> Focus.set(lisa, “Snowball II”) %{name: “Lisa”, pets: %{cat: “Snowball II”}}
safe_view(Lens.t, Focus.Types.traversable) :: {:error, :bad_arg} | {:ok, any}
Get a piece of a data structure that a lens Focuses on; returns {:ok, data} | {:error, :bad_lens_path}
Examples
iex> marge = %{name: "Marge", address: %{street: "123 Fake St.", city: "Springfield"}}
iex> name_lens = Lens.make_lens(:name)
iex> Lens.safe_view(name_lens, marge)
{:ok, "Marge"}