flatMap<A, B> function
- Option<
B> f(- A
Transforms the value inside an Option using a function that returns an Option.
flatMap
is used to apply a function that returns an Option to the value wrapped inside another Option.
If the input Option is None, the result will also be None.
If the input Option contains a value, the function will be applied to that value, and the resulting Option is returned.
Example:
Option<int> someValue = Some(10);
var doubled = flatMap<int, int>((x) => Some(x * 2))(someValue); // Should result in Some(20)
var noneResult = flatMap<int, int>((x) => None())(someValue); // Should result in None
var noneInput = flatMap<int, int>((x) => Some(x + 5))(None()); // Should result in None
@param f A function that takes a value of type A
and returns an Option of type B
.
@return A function that takes an Option of type A
and returns an Option of type B
.
Implementation
Option<B> Function(Option<A>) flatMap<A, B>(Option<B> Function(A) f) =>
match<A, Option<B>>(() => None<B>(), (a) => f(a));