ExAequoFn (ExAequoFn v0.1.3)

View Source

Functional helpers

const_fn

A function that returns a const

Ignoring up to 3 additional args, returning a const

iex(1)> const_fn(1).()
1

iex(2)> const_fn(2, 1)
2

iex(3)> const_fn(:a, 1, 2)
:a

iex(4)> const_fn(nil, 1, :a, [])
nil

named_fn

By default they have arity 1

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)

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

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, ...)

iex(8)> nil_fn()
nil

iex(9)> nil_fn(42)
nil

iex(10)> nil_fn({:a, :b}, "hello")
nil

iex(11)> nil_fn([], "hello", %{})
nil

### tagged_fn

A function that wraps the result of const_fn into a tagged tuple

iex(12)> tagged_fn(:alpha).("beta")
{:alpha, "beta"}

### transform_many

delegates to ExAequoFn.Transformer.many (see below for details)

Summary

Types

binary?()

@type binary?() :: maybe(binary())

either_t(error_t, ok_t)

@type either_t(error_t, ok_t) :: {:ok, ok_t} | {:error, error_t}

function_t()

@type function_t() :: (... -> any())

maybe(t)

@type maybe(t) :: nil | t

result_t(t)

@type result_t(t) :: {:ok, t} | :error

stream_t()

@type stream_t() :: %Stream{accs: term(), done: term(), enum: term(), funs: term()}

transformer_t()

@type transformer_t() :: (any() -> result_t(any()))

transformers_t()

@type transformers_t() :: [transformer_t()]

Functions

const_fn(const)

@spec const_fn(a) :: a when a: any()

const_fn(const, _)

@spec const_fn(a, any()) :: a when a: any()

const_fn(const, _, _)

@spec const_fn(a, any(), any()) :: a when a: any()

const_fn(const, _, _, _)

@spec const_fn(a, any(), any(), any()) :: a when a: any()

named_fn(fun, name \\ nil)

@spec named_fn(function_t(), binary?()) :: ExAequoFn.NamedFn.t()

See ExAequoFn.NamedFn.new/2.

nil_fn()

@spec nil_fn() :: nil

nil_fn(_)

@spec nil_fn(any()) :: nil

nil_fn(_, _)

@spec nil_fn(any(), any()) :: nil

nil_fn(_, _, _)

@spec nil_fn(any(), any(), any()) :: nil

tagged_fn(tag)

@spec tagged_fn(a) :: (b -> {a, b}) when a: any(), b: any()

transform_many(input, transformations)

See ExAequoFn.Transformer.many/2.