plymio_funcio v0.1.0 Plymio.Funcio.Predicate.Utility View Source

Predicate Utility Functions.

See Plymio.Funcio for an overview and documentation terms.

Link to this section Summary

Functions

validate_predicate1_fun/1 validates the argument is an arity 1 function, returning {:ok, fun} or {:error, error}

validate_predicate1_funs/1 validates the argument is a list of arity 1 functions, returning {:ok, funs} or {:error, error}

Link to this section Types

Link to this section Functions

Link to this function validate_predicate1_fun(fun) View Source
validate_predicate1_fun(any()) :: {:ok, fun1_predicate()} | {:error, error()}

validate_predicate1_fun/1 validates the argument is an arity 1 function, returning {:ok, fun} or {:error, error}.

Examples

iex> fun1 = fn v -> is_integer(v) end
...> {:ok, fun2} = fun1 |> validate_predicate1_fun
...> fun2 |> is_function(1)
true

iex> fun1 = fn _k,v -> v end
...> {:error, error} = fun1 |> validate_predicate1_fun
...> error |> Exception.message |> String.starts_with?("predicate/1 function invalid")
true

iex> fun1 = 42
...> {:error, error} = fun1 |> validate_predicate1_fun
...> error |> Exception.message
"predicate/1 function invalid, got: 42"
Link to this function validate_predicate1_funs(funs) View Source
validate_predicate1_funs(any()) ::
  {:ok, [fun1_predicate()]} | {:error, error()}

validate_predicate1_funs/1 validates the argument is a list of arity 1 functions, returning {:ok, funs} or {:error, error}.

Examples

iex> fun1 = fn v -> is_integer(v) end
...> {:error, error} = fun1 |> validate_predicate1_funs
...> error |> Exception.message |> String.starts_with?("predicate/1 functions invalid")
true

iex> fun1 = [fn v -> is_integer(v) end]
...> {:ok, fun2} = fun1 |> validate_predicate1_funs
...> fun2 |> Enum.all?(&(is_function(&1,1)))
true

iex> fun1 = [fn v -> is_integer(v) end, fn v -> is_atom(v) end]
...> {:ok, fun2} = fun1 |> validate_predicate1_funs
...> fun2 |> Enum.all?(&(is_function(&1,1)))
true

iex> fun1 = [fn v -> is_integer(v) end, fn _k,v -> v end]
...> {:error, error} = fun1 |> validate_predicate1_funs
...> error |> Exception.message |> String.starts_with?("predicate/1 function invalid")
true