View Source Bunch.Access (Bunch v1.6.1)

A bunch of functions for easier manipulation on terms of types implementing Access behaviour.

Summary

Functions

Implements Access behaviour by delegating callbacks to Map module.

Works like pop_in/2, but discards returned value.

Works like Kernel.get_in/2 with small differences.

Updates value at keys in a nested data structure and returns new value and updated structure.

Works like Kernel.pop_in/2 with small differences.

Works like Kernel.put_in/3 with small differences.

Works like Kernel.update_in/3 with small differences.

Functions

Link to this macro

__using__(args)

View Source (macro)

Implements Access behaviour by delegating callbacks to Map module.

All the callbacks are overridable.

Link to this function

delete_in(container, keys)

View Source
@spec delete_in(Access.t(), Access.key() | [Access.key()]) :: Access.t()

Works like pop_in/2, but discards returned value.

Examples

iex> Bunch.Access.delete_in(%{a: %{b: 10}}, [:a, :b])
%{a: %{}}
iex> Bunch.Access.delete_in(%{a: 10}, :a)
%{}
iex> Bunch.Access.delete_in(10, [])
nil
Link to this function

get_and_update_in(container, keys, f)

View Source
@spec get_and_update_in(Access.t(), Access.key() | [Access.key()], (a -> {b, a})) ::
  {b, Access.t()}
when a: Access.value(), b: any()

Works like Kernel.get_and_update_in/3 with small differences.

Behaviour differs in the following aspects:

  • empty lists of keys are allowed
  • single key does not have to be wrapped in a list

Examples

iex> Bunch.Access.get_and_update_in(%{a: %{b: 10}}, [:a, :b], & {&1, &1 * 2})
{10, %{a: %{b: 20}}}
iex> Bunch.Access.get_and_update_in(%{a: 10}, :a, & {&1, &1 * 2})
{10, %{a: 20}}
iex> Bunch.Access.get_and_update_in(10, [], & {&1, &1 * 2})
{10, 20}
@spec get_in(Access.t(), Access.key() | [Access.key()]) :: Access.value()

Works like Kernel.get_in/2 with small differences.

Behaviour differs in the following aspects:

  • empty lists of keys are allowed
  • single key does not have to be wrapped in a list

Examples

iex> Bunch.Access.get_in(%{a: %{b: 10}}, [:a, :b])
10
iex> Bunch.Access.get_in(%{a: 10}, :a)
10
iex> Bunch.Access.get_in(%{a: %{b: 10}}, [])
%{a: %{b: 10}}
Link to this function

get_updated_in(container, keys, f)

View Source
@spec get_updated_in(Access.t(), Access.key() | [Access.key()], (Access.value() -> a)) ::
  {a, Access.t()}
when a: Access.value()

Updates value at keys in a nested data structure and returns new value and updated structure.

Uses get_and_update_in/3 under the hood.

Example

iex> %{a: %{b: 10}} |> Bunch.Access.get_updated_in([:a, :b], & &1+1)
{11, %{a: %{b: 11}}}
@spec pop_in(Access.t(), Access.key() | [Access.key()]) ::
  {Access.value(), Access.t()}

Works like Kernel.pop_in/2 with small differences.

Behaviour differs in the following aspects:

  • empty lists of keys are allowed
  • single key does not have to be wrapped in a list

Examples

iex> Bunch.Access.pop_in(%{a: %{b: 10}}, [:a, :b])
{10, %{a: %{}}}
iex> Bunch.Access.pop_in(%{a: 10}, :a)
{10, %{}}
iex> Bunch.Access.pop_in(10, [])
{10, nil}
Link to this function

put_in(container, keys, v)

View Source
@spec put_in(Access.t(), Access.key() | [Access.key()], Access.value()) ::
  Access.value()

Works like Kernel.put_in/3 with small differences.

Behaviour differs in the following aspects:

  • empty lists of keys are allowed
  • single key does not have to be wrapped in a list

Examples

iex> Bunch.Access.put_in(%{a: %{b: 10}}, [:a, :b], 20)
%{a: %{b: 20}}
iex> Bunch.Access.put_in(%{a: 10}, :a, 20)
%{a: 20}
iex> Bunch.Access.put_in(%{a: %{b: 10}}, [], 20)
20
Link to this function

update_in(container, keys, f)

View Source
@spec update_in(
  Access.t(),
  Access.key() | [Access.key()],
  (Access.value() -> Access.value())
) ::
  Access.t()

Works like Kernel.update_in/3 with small differences.

Behaviour differs in the following aspects:

  • empty lists of keys are allowed
  • single key does not have to be wrapped in a list

Examples

iex> Bunch.Access.update_in(%{a: %{b: 10}}, [:a, :b], & &1 * 2)
%{a: %{b: 20}}
iex> Bunch.Access.update_in(%{a: 10}, :a, & &1 * 2)
%{a: 20}
iex> Bunch.Access.update_in(10, [], & &1 * 2)
20