https://github.com/kabie/named_fn
Named function for Elixir.
https://github.com/kabie/named_fn
elixir elixir-lang
Last synced: 10 months ago
JSON representation
Named function for Elixir.
- Host: GitHub
- URL: https://github.com/kabie/named_fn
- Owner: Kabie
- License: mit
- Created: 2020-08-16T09:27:47.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2022-01-09T14:39:31.000Z (over 4 years ago)
- Last Synced: 2025-04-04T07:43:58.203Z (about 1 year ago)
- Topics: elixir, elixir-lang
- Language: Elixir
- Homepage:
- Size: 5.86 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# NamedFn
Named function for Elixir.
Erlang added named fun for some time. While Elixir havn't adopt it yet. This package is a workaround.
## Usage
Equivalent to:
```erlang
Factorial = fun
F(0) -> 1;
F(X) when X > 0 -> X * F(X - 1)
end.
```
We have:
```elixir
factorial = named_fn :f do
0 -> 1
x when x > 0 -> x * f(x - 1)
end
```
## Limitations
Currently this heavily depend on Elixir and Erlang compiler. So it may break if underlaying APIs changed.
If you define a named function with 0 arity, you can't call it inside body. Since it's hard to distinguish a call and a var, and we want to support using the function as a var.
## Installation
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `named_fn` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:named_fn, "~> 0.1.0"}
]
end
```
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at [https://hexdocs.pm/named_fn](https://hexdocs.pm/named_fn).