View Source Pathex.Combinator (Pathex v2.6.0)

Combinator for lenses Read Pathex.Combinator.combine/1 documentation

Summary

Functions

This function creates a recursive path from path defined in path_func Consider this example

Functions

Link to this function

combine(path_func, max_depth \\ :infinity)

View Source
@spec combine((Pathex.t() -> Pathex.t()), pos_integer() | :infinity) :: Pathex.t()

This function creates a recursive path from path defined in path_func Consider this example

iex> import Pathex; import Pathex.Lenses
iex> recursive_xpath = combine(fn recursive_xpath ->
iex>   path(:x)            # Takes by :x key
iex>   ~> recursive_xpath  # If taken, calls itself
iex>   ||| matching(_)     # Otherwise returns current structure
iex> end)
iex>
iex> Pathex.view!(%{x: %{x: %{x: %{x: 1}}}}, recursive_xpath)
1
iex> Pathex.set!(%{x: %{x: %{x: %{x: 1}}}}, recursive_xpath, 2)
%{x: %{x: %{x: %{x: 2}}}}

The second argument of this function specifies the maximum depth. It's infinity be default, but you can specify this as any positive integer. It is useful when you're developing lens and you're not sure whether the lens will or won't loop.

For example

# Combinator lens with limit
limited = combine(fn rec -> path(:x) ~> rec end, 100_000)
:error = Pathex.force_set(%{x: 1}, limited, 123)

# And this is without limit
unlimited = combine(fn rec -> path(:x) ~> rec end)
Pathex.force_set(%{x: 1}, unlimited, 123) # Infinite loop