focus v0.2.0 Focus

Common functions usable by lenses, prisms, and traversals.

Summary

Functions

Compose a pair of lenses to operate at the same level as one another. Calling Focus.view/2, Focus.over/3, or Focus.set/3 on an alongside composed pair returns a two-element tuple of the result

Compose with most general lens on the left

Partially apply a lens to Focus.over/3, fixing the lens argument and returning a function that takes a Types.traversable and an update function

Partially apply a lens to Focus.set/3, fixing the optic argument and returning a function that takes a Types.traversable and a new value

Fix Focus.view/2 on a given optic. This partially applies Focus.view/2 with the given optic and returns a function that takes a Types.traversable structure

Check whether an optic’s target is present in a data structure

Check whether an optic’s target is not present in a data structure

Wrapper around Focusable.over/3

Wrapper around Focusable.set/3

Wrapper around Focusable.view/2

Given a list of lenses and a structure, apply Focus.view/2 for each lens to the structure

Infix lens composition

Functions

alongside(map1, map2)

Compose a pair of lenses to operate at the same level as one another. Calling Focus.view/2, Focus.over/3, or Focus.set/3 on an alongside composed pair returns a two-element tuple of the result.

Examples

iex> nums = [1,2,3,4,5,6]
iex> Focus.alongside(Prism.idx(0), Prism.idx(3))
...> |> Focus.view(nums)
{1, 4}

iex> bart = %{name: "Bart", parents: {"Homer", "Marge"}, age: 10}
iex> Focus.alongside(Lens.make_lens(:name), Lens.make_lens(:age))
...> |> Focus.view(bart)
{"Bart", 10}
compose(map1, map2)

Compose with most general lens on the left

Examples

iex> marge = %{
...>   name: "Marge",
...>   address: %{
...>     street: "123 Fake St.",
...>     city: "Springfield"
...>   }
...> }
iex> address_lens = Lens.make_lens(:address)
iex> street_lens = Lens.make_lens(:street)
iex> composed = Focus.compose(address_lens, street_lens)
iex> Focus.view(composed, marge)
"123 Fake St."
fix_over(lens, f \\ fn x -> x end)

Partially apply a lens to Focus.over/3, fixing the lens argument and returning a function that takes a Types.traversable and an update function.

Examples

iex> upcase_name = Lens.make_lens(:name)
...> |> Focus.fix_over(&String.upcase/1)
iex> %{name: "Bart", parents: {"Homer", "Marge"}}
...> |> upcase_name.()
%{name: "BART", parents: {"Homer", "Marge"}}

iex> fst = Prism.idx(0)
iex> states = [:maryland, :texas, :illinois]
iex> Focus.over(fst, states, &String.upcase(Atom.to_string(&1)))
["MARYLAND", :texas, :illinois]

Partially apply a lens to Focus.set/3, fixing the optic argument and returning a function that takes a Types.traversable and a new value.

Examples

iex> name_setter = Lens.make_lens(:name)
...> |> Focus.fix_set
iex> %{name: "Bart", parents: {"Homer", "Marge"}}
...> |> name_setter.("Lisa")
%{name: "Lisa", parents: {"Homer", "Marge"}}

iex> fst = Prism.idx(0)
iex> states = [:maryland, :texas, :illinois]
iex> Focus.over(fst, states, &String.upcase(Atom.to_string(&1)))
["MARYLAND", :texas, :illinois]
fix_view(optic)

Fix Focus.view/2 on a given optic. This partially applies Focus.view/2 with the given optic and returns a function that takes a Types.traversable structure.

Examples

iex> view_name = Lens.make_lens(:name)
...> |> Focus.fix_view
iex> homer = %{name: "Homer"}
iex> view_name.(homer)
"Homer"
iex> [homer, %{name: "Marge"}, %{name: "Bart"}]
...> |> Enum.map(&view_name.(&1))
["Homer", "Marge", "Bart"]
has(optic, structure)

Check whether an optic’s target is present in a data structure.

Examples

iex> first_elem = Prism.idx(1)
iex> first_elem |> Focus.has([0])
false

iex> name = Lens.make_lens(:name)
iex> name |> Focus.has(%{name: "Homer"})
true
hasnt(optic, structure)

Check whether an optic’s target is not present in a data structure.

Examples

iex> first_elem = Prism.idx(1)
iex> first_elem |> Focus.hasnt([0])
true

iex> name = Lens.make_lens(:name)
iex> name |> Focus.hasnt(%{name: "Homer"})
false
over(optic, structure, f)

Wrapper around Focusable.over/3

set(optic, structure, v)

Wrapper around Focusable.set/3

view(optic, structure)

Wrapper around Focusable.view/2

view_list(lenses, structure)

Given a list of lenses and a structure, apply Focus.view/2 for each lens to the structure.

Examples

iex> homer = %{
...>   name: "Homer",
...>   job: "Nuclear Safety Inspector",
...>   children: ["Bart", "Lisa", "Maggie"]
...> }
iex> lenses = Lens.make_lenses(homer)
iex> [lenses.name, lenses.children]
...> |> Focus.view_list(homer)
["Homer", ["Bart", "Lisa", "Maggie"]]

Infix lens composition

Examples

iex> import Focus
iex> marge = %{name: "Marge", address: %{
...>   local: %{number: 123, street: "Fake St."},
...>   city: "Springfield"}
...> }
iex> address_lens = Lens.make_lens(:address)
iex> local_lens = Lens.make_lens(:local)
iex> street_lens = Lens.make_lens(:street)
iex> address_lens ~> local_lens ~> street_lens |> Focus.view(marge)
"Fake St."