DEV Community

Alessandro Maclaine
Alessandro Maclaine

Posted on

Equivalence and Ordering of Options in Effect-TS: A Practical Guide

Effect-TS provides mechanisms to compare Options, 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 Options are equal or when you need to sort or compare them. In this article, we'll explore two key functions for comparing Options: O.getEquivalence and O.getOrder.

Example 1: Comparing Options for Equivalence with O.getEquivalence

Concept

The O.getEquivalence function creates an equivalence instance for Options, allowing you to compare the values inside them. It returns true if both Options 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)
}
Enter fullscreen mode Exit fullscreen mode

Explanation

  • optionEquivalence(O.some(1), O.some(1)): Both Options contain the value 1, so they are considered equivalent, resulting in true.
  • optionEquivalence(O.some(1), O.some(2)): The Options contain different values (1 and 2), so they are not equivalent, resulting in false.
  • optionEquivalence(O.none(), O.some(1)): One Option is None and the other contains a value, so they are not equivalent, resulting in false.
  • optionEquivalence(O.none(), O.none()): Both Options are None, so they are considered equivalent, resulting in true.

This function is useful when you need to check if two Options 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 Options, 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)
}
Enter fullscreen mode Exit fullscreen mode

Explanation

  • optionOrder(O.some(1), O.some(2)): The value 1 is less than 2, so the function returns -1.
  • optionOrder(O.some(2), O.some(1)): The value 2 is greater than 1, so the function returns 1.
  • optionOrder(O.some(1), O.some(1)): Both Options contain the same value (1), so the function returns 0.
  • optionOrder(O.none(), O.some(1)): None is considered less than Some, so the function returns -1.
  • optionOrder(O.some(1), O.none()): Some is considered greater than None, so the function returns 1.
  • optionOrder(O.none(), O.none()): Both Options are None, so they are considered equal, and the function returns 0.

This function is helpful when you need to sort or compare Options, ensuring a consistent ordering even when some values might be None.

Conclusion

Effect-TS offers powerful tools for comparing Options through equivalence and ordering. With O.getEquivalence, you can determine if two Options are the same, either by containing the same value or both being None. Meanwhile, O.getOrder allows you to establish a clear ordering among Options, 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)