Can you pattern match functions?

I have this function:

def apply_function(func, 0, nil, history), do: func.(value, key, history)
def apply_function(func, value, nil, history), do: func.(value, history)
def apply_function(func, value, key, history), do: func.(value, key, history)

It takes an anonymous function and several other values as arguments, then applies the function using said arguments.

Now, I want to check the number of arguments the anonymous functions accepts and apply it to just a subset of the arguments or raise an exception based on that.

I was thinking if it was possible to pattern match func and discover the number of arguments it can receive, or perhaps I’m overthinking this and the solution is much simpler ?

1 Like

There are is_function/1 and is_function/2, which are both guard-safe functions.

6 Likes

While reading about is_function/2 , I discovered :erlang.fun_info(func) which returns a bunch of information about a function, including arity.

2 Likes