ap<B> method
- covariant Option<
B Function(T t)> a
override
Apply the function contained inside a
to change the value of type T
to
a value of type B
.
final a = Option.of(10);
final b = Option.of(20);
/// `map` takes one parameter [int] and returns `sumToDouble`.
/// We therefore have a function inside a [Option] that we want to
/// apply to another value!
final Option<double Function(int)> map = a.map(
(a) => (int b) => sumToDouble(a, b),
);
/// Using `ap`, we get the final `Option<double>` that we want 🚀
final result = b.ap(map);
Implementation
@override
Option<B> ap<B>(covariant Option<B Function(T t)> a) => a.match(
() => Option<B>.none(),
(f) => map(f),
);