Well, I wouldn't go as far as actually using wrapFunction, but for example it could look like this:
(excuse the formatting, I don't think medium supports code blocks in comments)
return wrappedFunction()
.flatMap(this::foo)
.flatMap(this::bar);
// usually Result types also offer more functions that can be implemented using the give ones
// e.g. .orElse(DEFAULT_VALUE);
Instead of doing
String wrappedStr;
try {
wrappedStr = wrappedFunction()
} catch (Exception ex) {
return null; // if I want to catch it
}
return foo(bar(wrappedStr));
The standard java APIs even sometimes have a monad as result. For example the stream API sometimes returns an Optional:
list.stream()
.filter(this::someFilter)
.findFirst() // this returns an Optional – essentially a result without the exception
.default("default");