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 Option
s: 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))
: TheOption
contains the value1
, and the custom equivalence function confirms this, resulting intrue
. -
pipe(some1, O.containsWith(numberEquivalence)(2))
: TheOption
does not contain the value2
, so the result isfalse
. -
pipe(none, O.containsWith(numberEquivalence)(1))
: TheOption
isNone
, so the result isfalse
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))
: TheOption
contains the value1
, so the result istrue
. -
pipe(some1, O.contains(2))
: TheOption
does not contain the value2
, so the result isfalse
. -
pipe(none, O.contains(1))
: TheOption
isNone
, so the result isfalse
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 Option
s effectively, ensuring you can verify the presence of values in an intuitive and controlled manner.
Top comments (0)