"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Checking Elements in Options in Effect-TS: A Practical Guide

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

Published on 2024-11-08
Browse:720

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)
}

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)
}

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.

Release Statement This article is reproduced at: https://dev.to/almaclaine/checking-elements-in-options-in-effect-ts-a-practical-guide-3o6b?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3