Oscar Ablinger
Jan 21, 2022

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");

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Oscar Ablinger
Oscar Ablinger

Written by Oscar Ablinger

Hi, I’m a Software Engineer that just writes on here about whatever he finds out in a given week. Could be scripts, insights or explanations of things I learned

Responses (1)

Write a response