Effect-TS provides mechanisms to compare Option
s, allowing you to determine their equivalence or order based on the values they contain. These tools are useful when you need to check if two Option
s are equal or when you need to sort or compare them. In this article, we'll explore two key functions for comparing Option
s: O.getEquivalence
and O.getOrder
.
Example 1: Comparing Options for Equivalence with O.getEquivalence
Concept
The O.getEquivalence
function creates an equivalence instance for Option
s, allowing you to compare the values inside them. It returns true
if both Option
s are equivalent, meaning they either both contain the same value or are both None
.
Code
function equivalence_ex01() {
// Get the equivalence instance for numbers
const optionEquivalence = O.getEquivalence(Eq.number);
console.log(optionEquivalence(O.some(1), O.some(1))); // Output: true (both Options contain 1)
console.log(optionEquivalence(O.some(1), O.some(2))); // Output: false (Options contain different values)
console.log(optionEquivalence(O.none(), O.some(1))); // Output: false (one Option is None)
console.log(optionEquivalence(O.none(), O.none())); // Output: true (both Options are None)
}
Explanation
-
optionEquivalence(O.some(1), O.some(1))
: BothOption
s contain the value1
, so they are considered equivalent, resulting intrue
. -
optionEquivalence(O.some(1), O.some(2))
: TheOption
s contain different values (1
and2
), so they are not equivalent, resulting infalse
. -
optionEquivalence(O.none(), O.some(1))
: OneOption
isNone
and the other contains a value, so they are not equivalent, resulting infalse
. -
optionEquivalence(O.none(), O.none())
: BothOption
s areNone
, so they are considered equivalent, resulting intrue
.
This function is useful when you need to check if two Option
s are the same, either by having the same value or both being None
.
Example 2: Ordering Options with O.getOrder
Concept
The O.getOrder
function creates an order instance for Option
s, allowing you to compare and determine their order. This function returns -1
if the first Option
is less than the second, 1
if it is greater, and 0
if they are considered equal. None
is considered less than Some
.
Code
function order_ex01() {
// Get the order instance for numbers
const optionOrder = O.getOrder(Ord.number);
console.log(optionOrder(O.some(1), O.some(2))); // Output: -1 (1 is less than 2)
console.log(optionOrder(O.some(2), O.some(1))); // Output: 1 (2 is greater than 1)
console.log(optionOrder(O.some(1), O.some(1))); // Output: 0 (both Options contain 1)
console.log(optionOrder(O.none(), O.some(1))); // Output: -1 (None is less than Some)
console.log(optionOrder(O.some(1), O.none())); // Output: 1 (Some is greater than None)
console.log(optionOrder(O.none(), O.none())); // Output: 0 (both Options are None)
}
Explanation
-
optionOrder(O.some(1), O.some(2))
: The value1
is less than2
, so the function returns-1
. -
optionOrder(O.some(2), O.some(1))
: The value2
is greater than1
, so the function returns1
. -
optionOrder(O.some(1), O.some(1))
: BothOption
s contain the same value (1
), so the function returns0
. -
optionOrder(O.none(), O.some(1))
:None
is considered less thanSome
, so the function returns-1
. -
optionOrder(O.some(1), O.none())
:Some
is considered greater thanNone
, so the function returns1
. -
optionOrder(O.none(), O.none())
: BothOption
s areNone
, so they are considered equal, and the function returns0
.
This function is helpful when you need to sort or compare Option
s, ensuring a consistent ordering even when some values might be None
.
Conclusion
Effect-TS offers powerful tools for comparing Option
s through equivalence and ordering. With O.getEquivalence
, you can determine if two Option
s are the same, either by containing the same value or both being None
. Meanwhile, O.getOrder
allows you to establish a clear ordering among Option
s, considering None
as less than any Some
value. These functions enable precise and consistent comparisons, making them essential tools for managing optional values in a functional programming context.
Top comments (0)