View Source Funx.Eq.Utils (funx v0.1.0)
Utility functions for working with the Funx.Eq
protocol.
These functions assume that types passed in either support Elixir's equality operator
or implement the Funx.Eq
protocol.
Summary
Functions
Combines two equality comparators using the Eq.All
monoid.
Combines two equality comparators using the Eq.Any
monoid.
Concatenates a list of equality comparators using the Eq.All
monoid.
Concatenates a list of equality comparators using the Eq.Any
monoid.
Transforms an equality check by applying a function f
to values before comparison.
Returns true if two values are equal, using a specified or default Eq
.
Checks equality of values by applying a projection function, using a specified or default Eq
.
Returns false if two values are not equal, using a specified or default Eq
.
Converts an Eq
comparator into a single-argument predicate function for use in Enum
functions.
Types
Functions
@spec append_all(Funx.Monoid.Eq.All.t(), Funx.Monoid.Eq.All.t()) :: Funx.Monoid.Eq.All.t()
Combines two equality comparators using the Eq.All
monoid.
This function merges two equality comparisons, requiring both to return true
for the final result to be considered equal. This enforces a strict equality rule,
where all comparators must agree.
Examples
iex> eq1 = Funx.Eq.Utils.contramap(& &1.name)
iex> eq2 = Funx.Eq.Utils.contramap(& &1.age)
iex> combined = Funx.Eq.Utils.append_all(eq1, eq2)
iex> Funx.Eq.Utils.eq?(%{name: "Alice", age: 30}, %{name: "Alice", age: 30}, combined)
true
iex> Funx.Eq.Utils.eq?(%{name: "Alice", age: 30}, %{name: "Alice", age: 25}, combined)
false
@spec append_any(Funx.Monoid.Eq.Any.t(), Funx.Monoid.Eq.Any.t()) :: Funx.Monoid.Eq.Any.t()
Combines two equality comparators using the Eq.Any
monoid.
This function merges two equality comparisons, where at least one
must return true
for the final result to be considered equal.
Examples
iex> eq1 = Funx.Eq.Utils.contramap(& &1.name)
iex> eq2 = Funx.Eq.Utils.contramap(& &1.age)
iex> combined = Funx.Eq.Utils.append_any(eq1, eq2)
iex> Funx.Eq.Utils.eq?(%{name: "Alice", age: 30}, %{name: "Alice", age: 25}, combined)
true
iex> Funx.Eq.Utils.eq?(%{name: "Alice", age: 30}, %{name: "Bob", age: 25}, combined)
false
@spec concat_all([Funx.Monoid.Eq.All.t()]) :: Funx.Monoid.Eq.All.t()
Concatenates a list of equality comparators using the Eq.All
monoid.
The resulting comparator requires all comparators in the list to agree that two values are equal.
Examples
iex> eq1 = Funx.Eq.Utils.contramap(& &1.name)
iex> eq2 = Funx.Eq.Utils.contramap(& &1.age)
iex> combined = Funx.Eq.Utils.concat_all([eq1, eq2])
iex> Funx.Eq.Utils.eq?(%{name: "Alice", age: 30}, %{name: "Alice", age: 30}, combined)
true
iex> Funx.Eq.Utils.eq?(%{name: "Alice", age: 30}, %{name: "Alice", age: 25}, combined)
false
@spec concat_any([Funx.Monoid.Eq.Any.t()]) :: Funx.Monoid.Eq.Any.t()
Concatenates a list of equality comparators using the Eq.Any
monoid.
The resulting comparator allows any comparator in the list to determine equality, making it more permissive.
Examples
iex> eq1 = Funx.Eq.Utils.contramap(& &1.name)
iex> eq2 = Funx.Eq.Utils.contramap(& &1.age)
iex> combined = Funx.Eq.Utils.concat_any([eq1, eq2])
iex> Funx.Eq.Utils.eq?(%{name: "Alice", age: 30}, %{name: "Alice", age: 25}, combined)
true
iex> Funx.Eq.Utils.eq?(%{name: "Alice", age: 30}, %{name: "Bob", age: 25}, combined)
false
Transforms an equality check by applying a function f
to values before comparison.
The eq
parameter can be an Eq
module or a custom comparator map with an :eq?
function.
If an Eq
module is provided, it wraps the module’s function to apply f
to each value before invoking the equality check.
If a custom comparator map is provided, it wraps the function in the map to apply f
to each value.
Examples
iex> eq = Funx.Eq.Utils.contramap(& &1.age)
iex> eq.eq?.(%{age: 30}, %{age: 30})
true
iex> eq.eq?.(%{age: 30}, %{age: 25})
false
Returns true if two values are equal, using a specified or default Eq
.
Examples
iex> Funx.Eq.Utils.eq?(42, 42)
true
iex> Funx.Eq.Utils.eq?("foo", "bar")
false
Checks equality of values by applying a projection function, using a specified or default Eq
.
The eq
parameter can be an Eq
module or a custom comparator map with an :eq?
function.
Examples
iex> Funx.Eq.Utils.eq_by?(& &1.age, %{age: 30}, %{age: 30})
true
iex> Funx.Eq.Utils.eq_by?(& &1.age, %{age: 30}, %{age: 25})
false
Returns false if two values are not equal, using a specified or default Eq
.
Examples
iex> Funx.Eq.Utils.not_eq?(42, 99)
true
iex> Funx.Eq.Utils.not_eq?("foo", "foo")
false
Converts an Eq
comparator into a single-argument predicate function for use in Enum
functions.
The resulting predicate takes a single element and returns true
if it matches the target
based on the specified Eq
. If no custom Eq
is provided, it defaults to Funx.Eq
.
Examples
iex> eq = Funx.Eq.Utils.contramap(& &1.name)
iex> predicate = Funx.Eq.Utils.to_predicate(%{name: "Alice"}, eq)
iex> Funx.Filterable.filter([%{name: "Alice"}, %{name: "Bob"}], predicate)
[%{name: "Alice"}]