map<U> method
Maps a Result<T, E>
to Result<U, E>
by applying a function to a
contained Ok
(success) value, and leaving an Err
(failure) value
untouched.
This function can be used to compose the results of two functions.
Examples
Result<int, String> parseToInt(String value) {
try {
return Ok(int.parse(value));
} catch (_) {
return Err(value);
}
}
Result<bool, String> isStringGreaterThanFive(String value) {
return parseToInt(value).map((int val) => val > 5);
}
expect(isStringGreaterThanFive("7"), Ok<bool, String>(true));
expect(isStringGreaterThanFive("4"), Ok<bool, String>(false));
expect(isStringGreaterThanFive("five"), Errbool, String>("five"));
Implementation
@override
Result<U, E> map<U>(U Function(T value) okMap) {
return Ok(okMap(v));
}