Variadic (Variadic functions v1.0.0)

Simulates Variadic functions in Elixir (i.e functions with an unknown number of arguments)

Arguments will named arg1, arg2....argN where N is @max_arity.

Uninitialized arguments are set to :no_args_at_this_position

example

Example:

defmodule MyTestModule do

  import Variadic

  ##
  ## Functions are defined as defv (they will be public)
  ##
  defv :test_function do
    # If arguments needed as a list or need arity then binding() 
    # should be the first thing called
    arguments = args_to_list(binding())
    work = arg1 + arg2
    {arg1, arg2, arg3, arg4, work, arguments}
  end

  defv :other_function do
    binding = binding()
    ## Do work
    x = 1 + 2 + 3
    arguments = args_to_list(binding)
    arity = get_arity(binding)
    {arg1, arg2, x, [arity: arity, arguments: arguments]}
  end
end

pass-3-arguments-note-arg4-is-no_args_at_this_position

Pass 3 arguments (note arg4 is :no_args_at_this_position):

iex> MyTestModule.test_function(1, 2, :hello)
{1, 2, :hello, :no_args_at_this_position, 3, [1, 2, :hello]}

pass-10-arguments

Pass 10 arguments:

iex> MyTestModule.test_function(1, 2, :hello, 4, 5, 6, :bye, %{key: 123}, [771,"something"], 10)
{1, 2, :hello, 4, 3, [1, 2, :hello, 4, 5, 6, :bye, %{key: 123}, [771, "something"], 10]}

show-binding

Show binding:

iex> MyTestModule.other_function(777, 888, 999)
{777, 888, 6, [arity: 3, arguments: [777, 888, 999]]}

helper-functions

Helper functions:

get_arity/1     # Returns the number of valid (set) arguments
args_to_list/1  # Returns a list of valid (set) arguments

Link to this section Summary

Functions

Returns the ordered list of set arguments

Returns the arity of the set arguments

Link to this section Functions

Link to this function

args_to_list(binding)

Returns the ordered list of set arguments

arguments = args_to_list(binding())

The function binding should be the first function called

Link to this macro

defv(name, list)

(macro)
Link to this function

get_arity(binding)

Returns the arity of the set arguments

arity = get_arity(binding())

The function binding should be the first function called