mapOrElse<U> method

Option<U> mapOrElse<U>(
  1. U orFn(),
  2. U mapFn(
    1. T
    )
)

Maps this Option<T> to an Option<U> using the given mapFn function with the held value if this Option is Some. Otherwise returns the result of orFn as Some(orFn()).

orFn will only be evaluated if this Option is None.

Option<int> a = Some(1);
Option<int> b = None();

print(a.mapOrElse(() => 5, (val) => val + 1).unwrap()); // prints: 2
print(b.mapOrElse(() => 5, (val) => val + 1).unwrap()); // prints: 5

Note: Unlike Rust's Option::map_or_else(), this method returns an Option value. Given that Option.map() returns the mapped Option it just made sense for this method to do the same.

See also: Rust: Option::map_or_else()

Implementation

Option<U> mapOrElse<U>(U Function() orFn, U Function(T) mapFn) => switch (this) {
	Some(value: T value) => Some(mapFn(value)),
	None() => Some(orFn())
};