Checking Elements in Options in Effect-TS: A Practical Guide

almaclaine

Alessandro Maclaine

Posted on September 18, 2024

Checking Elements in Options in Effect-TS: A Practical Guide

Effect-TS provides methods to check if an Option contains a specific value. These functions allow you to determine the presence of a value within an Option, either using a custom equivalence function or the default equivalence. In this article, we'll explore two key functions for checking elements in Options: O.containsWith and O.contains.

Example 1: Checking Elements with Custom Equivalence using O.containsWith

Concept

The O.containsWith function checks if an Option contains a specified value by using a custom equivalence function. This function returns true if the Option contains the value according to the provided equivalence; otherwise, it returns false.

Code

function elements_ex01() {
  const numberEquivalence = Eq.number;

  const some1 = O.some(1); // Create an Option containing the value 1
  const none = O.none(); // Create an Option representing no value

  console.log(pipe(some1, O.containsWith(numberEquivalence)(1))); // Output: true (Option contains 1)
  console.log(pipe(some1, O.containsWith(numberEquivalence)(2))); // Output: false (Option does not contain 2)
  console.log(pipe(none, O.containsWith(numberEquivalence)(1))); // Output: false (Option is None)
}
Enter fullscreen mode Exit fullscreen mode

Explanation

  • pipe(some1, O.containsWith(numberEquivalence)(1)): The Option contains the value 1, and the custom equivalence function confirms this, resulting in true.
  • pipe(some1, O.containsWith(numberEquivalence)(2)): The Option does not contain the value 2, so the result is false.
  • pipe(none, O.containsWith(numberEquivalence)(1)): The Option is None, so the result is false regardless of the value checked.

This function is useful when you need to check if an Option contains a specific value with a custom comparison logic, allowing for more flexibility in determining equivalence.

Example 2: Checking Elements with Default Equivalence using O.contains

Concept

The O.contains function checks if an Option contains a specified value using the default equivalence. It returns true if the Option contains the value; otherwise, it returns false. This function is simpler to use when you don't need custom comparison logic.

Code

function elements_ex02() {
  const some1 = O.some(1); // Create an Option containing the value 1
  const none = O.none(); // Create an Option representing no value

  console.log(pipe(some1, O.contains(1))); // Output: true (Option contains 1)
  console.log(pipe(some1, O.contains(2))); // Output: false (Option does not contain 2)
  console.log(pipe(none, O.contains(1))); // Output: false (Option is None)
}
Enter fullscreen mode Exit fullscreen mode

Explanation

  • pipe(some1, O.contains(1)): The Option contains the value 1, so the result is true.
  • pipe(some1, O.contains(2)): The Option does not contain the value 2, so the result is false.
  • pipe(none, O.contains(1)): The Option is None, so the result is false regardless of the value checked.

This function is useful for quickly checking if an Option contains a specific value when the default equivalence suffices, making it straightforward and easy to use.

Conclusion

Effect-TS provides efficient ways to check if an Option contains a specific value. With O.containsWith, you can use custom equivalence functions to define how the comparison should be made, offering flexibility for complex scenarios. Meanwhile, O.contains provides a simpler approach, leveraging the default equivalence for straightforward checks. These functions allow you to handle Options effectively, ensuring you can verify the presence of values in an intuitive and controlled manner.

💖 💪 🙅 🚩
almaclaine
Alessandro Maclaine

Posted on September 18, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related