plymio_funcio v0.2.0 Plymio.Funcio.Predicate View Source
Predicate Functions.
See Plymio.Funcio for an overview and documentation terms.
Link to this section Summary
Functions
reduce_and_predicate1_funs/1 validates the argument is a one or
more predicate/1 functions and reduces them to a single
predicate/1 function, returning {:ok, predicate}
reduce_or_predicate1_funs/1 validates the argument is a one or
more predicate/1 functions and reduces them to a single
predicate/1 function, returning {:ok, predicate}
Link to this section Types
Link to this section Functions
reduce_and_predicate1_funs(any()) :: {:ok, fun1_predicate()} | {:error, error()}
reduce_and_predicate1_funs/1 validates the argument is a one or
more predicate/1 functions and reduces them to a single
predicate/1 function, returning {:ok, predicate}.
Multiple predicates are AND-ed together to create a composite
predicate will return true only if all of the individual
predicates return true.
An AND-ed predicate can be used with e.g. Enum.filter/2 or Enum.reject/2.
Examples
iex> predicate1 = fn v -> is_integer(v) end
...> {:ok, and_predicate} = predicate1 |> reduce_and_predicate1_funs
...> false = :not_an_integer |> and_predicate.()
...> false = 3.14 |> and_predicate.()
...> true = 42 |> and_predicate.()
true
iex> predicates = [
...> fn v -> is_integer(v) end,
...> fn v -> v >= 5 end,
...> fn v -> v < 10 end,
...> ]
...> {:ok, and_predicate} = predicates |> reduce_and_predicate1_funs
...> false = :not_an_integer |> and_predicate.()
...> false = 3.14 |> and_predicate.()
...> false = 4 |> and_predicate.()
...> false = 10 |> and_predicate.()
...> true = 5 |> and_predicate.()
...> true = 6 .. 9 |> Enum.all?(and_predicate)
true
iex> predicates = [fn v -> is_integer(v) end, fn _k,v -> v end]
...> {:error, error} = predicates |> reduce_and_predicate1_funs
...> error |> Exception.message |> String.starts_with?("predicate/1 function invalid")
true
reduce_or_predicate1_funs(any()) :: {:ok, fun1_predicate()} | {:error, error()}
reduce_or_predicate1_funs/1 validates the argument is a one or
more predicate/1 functions and reduces them to a single
predicate/1 function, returning {:ok, predicate}.
Multiple predicates are OR-ed together to create a composite
predicate will return true if any of the individual predicates
return true.
An OR-ed predicate can be used with e.g. Enum.filter/2.
When using a composite OR predicate with e.g. Enum.reject/2, it
should be remembered that if any of individual predicates
returns true, the composite predicate will return true and the
reject “fail”.
Examples
iex> predicate1 = fn v -> is_integer(v) end
...> {:ok, or_predicate} = predicate1 |> reduce_or_predicate1_funs
...> false = :not_an_integer |> or_predicate.()
...> false = 3.14 |> or_predicate.()
...> true = 42 |> or_predicate.()
true
iex> predicates = [
...> fn v -> is_atom(v) end,
...> fn v -> is_binary(v) end,
...> fn v -> is_integer(v) && v >= 5 end,
...> fn v -> is_integer(v) && v < 10 end,
...> ]
...> {:ok, or_predicate} = predicates |> reduce_or_predicate1_funs
...> true = :not_an_integer |> or_predicate.()
...> true = "Hello World" |> or_predicate.()
...> false = 3.14 |> or_predicate.()
...> true = 4 |> or_predicate.()
...> true = 10 |> or_predicate.()
...> true = 5 |> or_predicate.()
...> true = 6 .. 9 |> Enum.any?(or_predicate)
true
iex> predicates = [fn v -> is_integer(v) end, fn _k,v -> v end]
...> {:error, error} = predicates |> reduce_or_predicate1_funs
...> error |> Exception.message |> String.starts_with?("predicate/1 function invalid")
true