Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nhpip/variadic-elixir
A simple implementation of Variadic functions in Elixir
https://github.com/nhpip/variadic-elixir
elixir variadic-function
Last synced: about 2 months ago
JSON representation
A simple implementation of Variadic functions in Elixir
- Host: GitHub
- URL: https://github.com/nhpip/variadic-elixir
- Owner: nhpip
- License: mit
- Created: 2021-06-07T23:02:45.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-06-24T20:36:28.000Z (over 2 years ago)
- Last Synced: 2024-09-14T22:21:44.717Z (4 months ago)
- Topics: elixir, variadic-function
- Language: Elixir
- Homepage:
- Size: 22.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Variadic
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 the atom :no_args_at_this_position
**NOTE:** Currently doesn't support guards in the function head
## Example:
```
defmodule MyTestModule doimport 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}
enddefv :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
```
From the shell:
```
Pass 3 arguments (note arg4 is nil):
iex(2)> MyModule.test_function(1, 2, :hello)
{1, 2, :hello, :no_args_at_this_position, 3, [1, 2, :hello]}Pass 10 arguments:
iex(3)> MyModule.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
iex(10)> MyModule.other_function(777, 888, 999)
{777, 888, 6, [arity: 3, arguments: [777, 888, 999]]}
```Helper functions: `get_arity/1` and `args_to_list/1`