tap<A> function
- void f(
- A
Curries the tap function by taking a side effect function f
. It returns a
specialized version of the tap function that can be used to apply the side
effect on an Option<A> instance directly without specifying the side effect
function again.
Example:
void main() {
Option<String> someOption = Some("Hello");
Option<String> noneOption = const None();
// Create a specialized version of tap for printing strings
final printString = tap<String>((value) => print("Value inside Some: $value"));
// Use the specialized version of tap to print the value inside the Some
printString(someOption); // Output: Value inside Some: Hello
// Use the same specialized version of tap to print the value inside the None
printString(noneOption); // No output
}
Implementation
TapFunction<A> tap<A>(void Function(A) f) {
return (Option<A> option) {
if (option is Some<A>) {
final Some<A> some = option;
f(some.value);
}
return option;
};
}