ask/eq
Types
pub type Equivalence(a) =
fn(a, a) -> Bool
Functions
pub fn and(
first: fn(a, a) -> Bool,
second: fn(a, a) -> Bool,
) -> fn(a, a) -> Bool
Combine two equivalences into a new equivalence that returns True
if both
equivalences return True
.
pub fn contains(eq: fn(a, a) -> Bool) -> fn(List(a), a) -> Bool
Test if a value is a member of a list according to the given equivalence.
pub fn default() -> fn(a, a) -> Bool
Create a new equivalence that uses the default equality comparison.
pub fn list(eq: fn(a, a) -> Bool) -> fn(List(a), List(a)) -> Bool
Create a new equivalence for a list of values based on the given equivalence.
pub fn map_input(
over eq: fn(a, a) -> Bool,
with fun: fn(b) -> a,
) -> fn(b, b) -> Bool
Map the input of an equivalence to create a new equivalence.
Examples
This can be useful for constructing a custom equivalence of a record, given that we have an equivalence for one or more of its fields.
import ask/equivalence as eq
type User {
User(name: String, age: Int)
}
pub fn main() {
let user1 = User("alice", 30)
let user2 = User("alice", 24)
let is_username_equal =
eq.default()
|> eq.map_input(fn(user: User) { user.name })
is_username_equal(user1, user2) // -> True
}
pub fn negate(eq: fn(a, a) -> Bool) -> fn(a, a) -> Bool
Negate an equivalence to create a new equivalence that returns True
if the
original equivalence returns False
.
pub fn or(
first: fn(a, a) -> Bool,
second: fn(a, a) -> Bool,
) -> fn(a, a) -> Bool
Combine two equivalences into a new equivalence that returns True
if either
equivalence returns True
.
pub fn pair(
first: fn(a, a) -> Bool,
second: fn(b, b) -> Bool,
) -> fn(#(a, b), #(a, b)) -> Bool
Create a new equivalence for a pair of values based on the given equivalences.
pub fn trivial() -> fn(a, a) -> Bool
Create a new equivalence that always returns True
.
This is sometimes called the trivial or universal equivalence.