contramap<A, B> function
- B f(
- A a
Transforms an Eq<B>
into an Eq<A>
using the given function.
Example:
void main() {
// Define an Eq for comparing strings case-insensitively
final eqIgnoreCase = fromEquals<String>((x, y) => x.toLowerCase() == y.toLowerCase());
// Define a function to extract the first character of a string
String firstChar(String s) => s.substring(0, 1);
// Create a contramapped Eq that compares strings by their first character
final eqByFirstChar = contramap<String, String>(firstChar)(eqIgnoreCase);
print(eqByFirstChar.equals("hello", "Hi")); // Output: true (Both start with 'h')
print(eqByFirstChar.equals("world", "war")); // Output: false ('w' is not equal to 'W')
}
Implementation
Eq<A> Function(Eq<B>) contramap<A, B>(B Function(A a) f) {
return (Eq<B> eqB) => _Eq<A>((A x, A y) => eqB.equals(f(x), f(y)));
}