Pathex v0.3.0 Pathex View Source
This module contains macroses to be used by user
Any macro here belongs to one of two categories:
1) Macro which creates path closure (sigil_P/2
, path/2
, ~>/2
)
2) Macro which uses path closure as path (over/3
, set/3
, view/2
)
Path closure is a closure which takes two arguments:
1) Atom.t()
with operaion name
2) Tuple.t()
of arguments of this operation
Link to this section Summary
Functions
Macro of three arguments which sets the given value in the given path of given structure
Macro of three arguments which applies given function for item in the given path of given structure
Creates path for given structure
Macro of three arguments which sets the given value in the given path of given structure
Sigil for paths. Has only two modes:
naive
(default) and json
.
Naive paths should look like ~P["string"/:atom/1]
Json paths should look like ~P[string/this_one_is_too/1/0]
Macro gets the value in the given path of the given structure
Creates composition of two paths
Link to this section Types
t()
View Sourcet() :: (:get | :set | :update | :force_set, {el_structure()} | {el_structure(), any()} | {el_structure(), (any() -> any())} -> result())
Link to this section Functions
Macro of three arguments which sets the given value in the given path of given structure
If the path does not exist it creates the path favouring maps when structure is unknown
Example:
iex> require Pathex; import Pathex
iex> x = 1
iex> {:ok, [0, %{x: 123}]} = force_set x / :x, [0, %{x: 8}], 123
iex> p = path "hey" / 0
iex> {:ok, %{"hey" => %{0 => 1}}} = force_set p, %{}, 1
Macro of three arguments which applies given function for item in the given path of given structure
Example:
iex> require Pathex; import Pathex
iex> x = 1
iex> inc = fn x -> x + 1 end
iex> {:ok, [0, %{x: 9}]} = over x / :x, [0, %{x: 8}], inc
iex> p = path "hey" / 0
iex> {:ok, %{"hey" => [2, [2]]}} = over p, %{"hey" => [1, [2]]}, inc
Creates path for given structure
Example:
iex> require Pathex; import Pathex
iex> x = 1
iex> mypath = path 1 / :atom / "string" / {"tuple?"} / x
iex> structure = [0, [atom: %{"string" => %{{"tuple?"} => %{1 => 2}}}]]
iex> {:ok, 2} = view mypath, structure
Macro of three arguments which sets the given value in the given path of given structure
Example:
iex> require Pathex; import Pathex
iex> x = 1
iex> {:ok, [0, %{x: 123}]} = set x / :x, [0, %{x: 8}], 123
iex> p = path "hey" / 0
iex> {:ok, %{"hey" => [123, [2]]}} = set p, %{"hey" => [1, [2]]}, 123
Sigil for paths. Has only two modes:
naive
(default) and json
.
Naive paths should look like ~P["string"/:atom/1]
Json paths should look like ~P[string/this_one_is_too/1/0]
Macro gets the value in the given path of the given structure
Example:
iex> require Pathex; import Pathex
iex> x = 1
iex> {:ok, 8} = view x / :x, [0, %{x: 8}]
iex> p = path "hey" / 0
iex> {:ok, 9} = view p, %{"hey" => {9, -9}}
Creates composition of two paths
Example:
iex> require Pathex; import Pathex
iex> p1 = path :x / :y
iex> p2 = path :a / :b
iex> composed_path = p1 ~> p2
iex> {:ok, 1} = view composed_path, %{x: [y: [a: [a: 0, b: 1]]]}