Delegator.AritiesMap (delegator v0.2.0)

Copy Markdown View Source

Data structure to record the arities of functions to include/exclude from delegation.

It uses a regular map, where keys are the function names as strings, and the values are either :* or a set of arities for that function.

All functions treat atoms and strings equally, i.e. function name :a is the same as function name "a".

Summary

Functions

Returns the number of entries in the arities map.

Fetch the arities for a specific function name.

Get the arities for a specific function name.

Checks whether an entry exists for a function name in the arities map.

Build a new arities map.

Add function to an arities map.

Invokes a function for each entry in the arities map with the accumulator.

Returns a function that slices the arities map contiguously.

Returns a list of pairs with the function name-arities pairs in the entries.

Functions

count(am)

Returns the number of entries in the arities map.

fetch(arities_map, fun_name)

Fetch the arities for a specific function name.

Returns {:ok, arities} if there is an entry for the provided function name, otherwise returns :error.

Examples

iex> Delegator.AritiesMap.new(:a)
...> |> Delegator.AritiesMap.fetch(:a)
{:ok, :*}

iex> Delegator.AritiesMap.new(:a)
...> |> Delegator.AritiesMap.fetch("a")
{:ok, :*}

iex> Delegator.AritiesMap.new(a: 1, a: 2)
...> |> Delegator.AritiesMap.fetch("a")
{:ok, [1, 2]}

iex> Delegator.AritiesMap.new()
...> |> Delegator.AritiesMap.fetch(:a)
:error

get(arities_map, fun_name, default \\ nil)

Get the arities for a specific function name.

If there's no entry for the provided function name, returns the default value.

Examples

iex> Delegator.AritiesMap.new(:a)
...> |> Delegator.AritiesMap.get(:a)
:*

iex> Delegator.AritiesMap.new(:a)
...> |> Delegator.AritiesMap.get("a")
:*

iex> Delegator.AritiesMap.new(a: 1, a: 2)
...> |> Delegator.AritiesMap.get("a")
[1, 2]

iex> Delegator.AritiesMap.new()
...> |> Delegator.AritiesMap.get(:a)
nil

iex> Delegator.AritiesMap.new()
...> |> Delegator.AritiesMap.get(:a, :not_found)
:not_found

member?(am, fun_name)

Checks whether an entry exists for a function name in the arities map.

Membership is tested with the match (===/2) operator.

new(arities \\ [])

Build a new arities map.

If provided, arities may be a single function name, or an enumerablei. As an enumerable it may contain function names, or function name-arity pairs, in any combination.

Names without arity, or arities using the wildcard :* will affect all functions with that name, regardless of the arity. Such an entry supersedes any other entries with a similar function name.

Examples

iex> Delegator.AritiesMap.new()
#Delegator.AritiesMap<entries: %{}>

iex> Delegator.AritiesMap.new(:a)
#Delegator.AritiesMap<entries: %{"a" => :*}>

iex> Delegator.AritiesMap.new(a: 1, a: 2)
#Delegator.AritiesMap<entries: %{"a" => [1, 2]}>

iex> Delegator.AritiesMap.new([:a, b: 1, b: 2])
#Delegator.AritiesMap<entries: %{"a" => :*, "b" => [1, 2]}>

put(arities_map, fun_name, fun_arity \\ :*)

Add function to an arities map.

The provided function arity is add to a set under the function name key.

The wildcard value overwrites any pre-defined set, and can not be overwritten.

Examples

iex> Delegator.AritiesMap.put(%Delegator.AritiesMap{}, :a)
#Delegator.AritiesMap<entries: %{"a" => :*}>

iex> Delegator.AritiesMap.put(%Delegator.AritiesMap{}, :a, 1)
#Delegator.AritiesMap<entries: %{"a" => [1]}>

iex> %Delegator.AritiesMap{}
...> |> Delegator.AritiesMap.put(:a, 1)
...> |> Delegator.AritiesMap.put(:a)
...> |> Delegator.AritiesMap.put(:a, 2)
#Delegator.AritiesMap<entries: %{"a" => :*}>

reduce(am, acc, fun)

Invokes a function for each entry in the arities map with the accumulator.

The initial value of the accumulator is acc. The function is invoked for each entry in the enumerable with the accumulator. The result returned by the function is used as the accumulator for the next iteration. The function returns the last accumulator.

slice(am)

Returns a function that slices the arities map contiguously.

to_list(am)

Returns a list of pairs with the function name-arities pairs in the entries.

Pairs are ordered by the function name, alphabetically.

Examples

iex> Delegator.AritiesMap.new()
...> |> Delegator.AritiesMap.to_list()
[]

iex> Delegator.AritiesMap.new(a: 1, b: :*)
...> |> Delegator.AritiesMap.to_list()
[{"a", [1]}, {"b", :*}]

iex> Delegator.AritiesMap.new(b: :*, a: 1)
...> |> Delegator.AritiesMap.to_list()
[{"a", [1]}, {"b", :*}]