<!--
DO NOT EDIT THIS FILE
It has been generated from the template `README.md.eex` by Extractly (https://github.com/RobertDober/extractly.git)
and any changes you make in this file will most likely be lost
-->
[](https://github.com/RobertDober/ex_aequo_fn/actions)
[](https://hex.pm/packages/ex_aequo_fn)
[](https://hex.pm/packages/ex_aequo_fn)
[](https://hex.pm/packages/ex_aequo_fn)
# ExAequoFn
Functional Tools and Helpers
## Documentation
Functional helpers
### `const_fn`
A function that returns a const
Ignoring up to 3 additional args, returning a const
```elixir
iex(1)> const_fn(1).()
1
```
```elixir
iex(2)> const_fn(2, 1)
2
```
```elixir
iex(3)> const_fn(:a, 1, 2)
:a
```
```elixir
iex(4)> const_fn(nil, 1, :a, [])
nil
```
### `named_fn`
By default they have arity 1
```elixir
iex(5)> add1 = named_fn(&(&1+1), "add1")
...(5)> assert add1.name == "add1"
...(5)> assert NamedFn.call(add1, 41) == 42
...(5)> assert NamedFn.call(add1, [41]) == 42
```
They can be curried, (but that might not be very useful with arity 1)
```elixir
iex(6)> add1 = named_fn(&(&1+1), "add_one")
...(6)> assert is_function(NamedFn.call(add1))
...(6)> assert is_function(NamedFn.call(add1, []))
...(6)> NamedFn.call(add1).(72)
73
```
But more useful with higher arities
```elixir
iex(7)> adder = named_fn(&(&1+&2), "adder")
...(7)> assert is_function(NamedFn.call(adder))
...(7)> add42 = NamedFn.call(adder, 42)
...(7)> add42.(31)
73
```
More examples can be found in the doctests of `NamedFn`
### `nil_fn`
Short for `const_fn(nil, ...)`
```elixir
iex(8)> nil_fn()
nil
```
```elixir
iex(9)> nil_fn(42)
nil
```
```elixir
iex(10)> nil_fn({:a, :b}, "hello")
nil
```
```elixir
iex(11)> nil_fn([], "hello", %{})
nil
```
### `tagged_fn`
A function that wraps the result of `const_fn` into a tagged tuple
```elixir
iex(12)> tagged_fn(:alpha).("beta")
{:alpha, "beta"}
```
### `transform_many`
delegates to `ExAequoFn.Transformer.many` (see below for details)
Implements functional transformation helpers
### `many`
`many` takes a list and a list of transsformers. A transformer is simply a function
that returns either `{:ok, value}` or `:error`
The list is traversed by calling all transformers on each element of the list until
the first transformer returns an `{:ok, value}` tuple in which case the element is
replaced by `value`. If a transformer returning an `:ok` value is found for **all**
elements, then the tuple `{:ok, [transformed values,...]}` is returned, otherwise
`:error` is returned
```elixir
iex(1)> ft = %{a: 1, b: 2}
...(1)> st = %{a: 2, x: 3}
...(1)> ff = &Map.fetch(ft, &1)
...(1)> sf = &Map.fetch(st, &1)
...(1)> assert many([:a, :x], [ff, sf]) == {:ok, [1, 3]}
...(1)> assert many([:a, :z], [ff, sf]) == :error
```
<!-- module not found Elixir.ExAequoFn.LazyIterator -->
## Author
Copyright © 2024 Robert Dober robert.dober@gmail.com
# LICENSE
GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007 or later. Please refer to [LICENSE](LICENSE) for details.
<!--SPDX-License-Identifier: AGPL-3.0-or-later-->