In functional programming, lifting refers to the process of adapting a function to work within a specific context, such as an Option
. Effect-TS provides convenient methods to lift functions into the Option
context, allowing you to apply functions or predicates to Option
values seamlessly. In this article, we'll explore two key functions for lifting: O.lift2
and O.liftPredicate
.
Example 1: Lifting a Binary Function with O.lift2
Concept
The O.lift2
function allows you to lift a binary function (a function that takes two arguments) into the Option
context. This enables you to apply the function to two Option
values. If both Option
s are Some
, the function is applied; if either Option
is None
, the result is None
.
Code
function lifting_ex01() {
const add = (a: number, b: number) => a + b;
const liftedAdd = O.lift2(add);
const some1 = O.some(1); // Create an Option containing the value 1
const some2 = O.some(2); // Create an Option containing the value 2
const none = O.none(); // Create an Option representing no value
console.log(pipe(some1, liftedAdd(some2))); // Output: Some(3) (1 + 2 = 3)
console.log(pipe(some1, liftedAdd(none))); // Output: None (since one Option is None)
console.log(pipe(none, liftedAdd(some2))); // Output: None (since one Option is None)
}
Explanation
-
pipe(some1, liftedAdd(some2))
: BothOption
s areSome
, so theadd
function is applied to the values, resulting inSome(3)
(1 + 2 = 3
). -
pipe(some1, liftedAdd(none))
: Since one of theOption
s isNone
, the result isNone
. -
pipe(none, liftedAdd(some2))
: Again, since one of theOption
s isNone
, the result isNone
.
This function is particularly useful when you want to apply a binary function to two Option
values while handling the cases where one or both Option
s might be None
.
Example 2: Lifting a Predicate Function with O.liftPredicate
Concept
The O.liftPredicate
function allows you to lift a predicate (a function that returns a boolean) into the Option
context. This converts the predicate into a function that returns an Option
. If the predicate is satisfied, it returns Some
with the value; otherwise, it returns None
.
Code
function lifting_ex02() {
const isPositive = (n: number) => n > 0;
const liftedIsPositive = O.liftPredicate(isPositive);
console.log(liftedIsPositive(1)); // Output: Some(1) (1 is positive)
console.log(liftedIsPositive(-1)); // Output: None (-1 is not positive)
console.log(liftedIsPositive(0)); // Output: None (0 is not positive)
}
Explanation
-
liftedIsPositive(1)
: The predicateisPositive
is satisfied (1 > 0
), so the result isSome(1)
. -
liftedIsPositive(-1)
: The predicate is not satisfied (-1
is not greater than0
), so the result isNone
. -
liftedIsPositive(0)
: The predicate is not satisfied (0
is not greater than0
), so the result isNone
.
This function is useful when you want to apply a predicate to a value and lift the result into the Option
context, returning Some
when the condition is met and None
otherwise.
Conclusion
Lifting functions into the Option
context in Effect-TS allows you to work seamlessly with optional values while applying functions or predicates. With O.lift2
, you can apply binary functions to Option
s, ensuring that operations are only performed when both values are present. Similarly, O.liftPredicate
enables you to convert predicates into functions that return an Option
, making it easy to encapsulate conditions within the Option
context. By using these tools, you can simplify your code and manage optional values more effectively in functional programming.
Top comments (2)
How to make the code block coloured ?
@zuhalcode put the language after the three ticks, '''typescript, but use back ticks