View Source Pathex.Accessibility (Pathex v2.6.0)

Helpers to create paths from Access.t() and lists

Note: This module is separate because functions presented in this module are suboptimal. You should try use Pathex.path/2 first, and only if its not applicable to you use-case, you should use functions from this module

Summary

Functions

Creates path-closure from Access.t().

Creates path from list of items. The list of items should not be known at runtime, therefore some optimizations of paths are not possible. Use this function only when Pathex.path/2 is not applicable

Creates a lens from record macro. All arguments of this macro must be compile time atoms

Creates a lens for specific key in the structure. It behaves the same way as a lens created with Pathex.path/2 but it works only with specified structure and force_set creates a structure with default values.

Converts path-closure to Access.t()

Types

@type access() :: [term(), ...]

Functions

@spec from_access(access()) :: Pathex.t()

Creates path-closure from Access.t().

Note: Paths created using this function do not support force operations yet

Example

iex> import Pathex
iex> p = from_access [:x, :y]
iex> 10 = view!(%{x: [y: 10]}, p)
Link to this function

from_list(list, mod \\ :naive)

View Source
@spec from_list([any()], Pathex.mod()) :: Pathex.t()

Creates path from list of items. The list of items should not be known at runtime, therefore some optimizations of paths are not possible. Use this function only when Pathex.path/2 is not applicable

Example

iex> import Pathex
iex> p = from_list [:x, 1, :y]
iex> 10 = view!(%{x: [1, [y: 10]]}, p)
Link to this macro

from_record(module, macro, key)

View Source (macro)

Creates a lens from record macro. All arguments of this macro must be compile time atoms

Example

iex> require Pathex
iex> defmodule User do
...>   import Record; defrecord(:user, name: "", age: nil)
...>
...>   def new(name) do
...>     name_lens = from_record(User, :user, :name)
...>     Pathex.force_set!({}, name_lens, name)
...>   end
...> end
iex> {:user, "Joe", nil} = User.new("Joe")
Link to this function

from_struct(module, key)

View Source
@spec from_struct(module(), atom()) ::
  Pathex.t(
    struct(),
    any()
  )

Creates a lens for specific key in the structure. It behaves the same way as a lens created with Pathex.path/2 but it works only with specified structure and force_set creates a structure with default values.

Example

iex> require Pathex
iex> defmodule User do
...>   defstruct name: "", age: nil
...> end
iex> name_lens = from_struct(User, :name)
iex> %{name: "Joe"} = Pathex.force_set!(%{}, name_lens, "Joe")
@spec to_access(Pathex.t()) :: [Access.access_fun(any(), any())]

Converts path-closure to Access.t()

Example

iex> import Pathex
iex> access = to_access path(:x / 0, :map)
iex> 1 = get_in(%{x: %{0 => 1}}, access)