Understanding the Monad in Java

Now in Java, with some efforts, you can also find monads.

Although, Java as a programming language was originally created completely different.

It was planned as a OOP language (with primitive types not to be too far from C++), and no monads were planned there at all.

Before the release of Java 8, Java and functional programming were completely different things.

But in Java 8, functions appeared that represent the transformation as a value.

Thus, Java 8 has become more functional.

There are quite a lot of professionals who do not believe or do not want to accept that the Java language is quite functional, and that it is possible to write a program in a functional style in Java.

They consider that it is necessary to do differently, to go to another programming language, where in fact “functional programming” is.

But this is not true.

Monads are more related to functional programming.

This is a container that accepts the current state and a function, which takes the current state as an argument and returns a new one.

Monads are quite difficult to explain in functional programming (as evidenced by existing study books on this subject in Haskell and Scala).

In fact, faced with various problems in functional programming you would be inevitably involved in certain decisions, among which monads are the most prominent examples.

Many of the problems that monads try to solve are related to the problem of side effects.

Please note that monads allow you to do more than handle side effects, in particular, many types of container objects can be viewed as monads.

Sometimes it is difficult to come to terms with the different uses of monads and to focus only on one or the other.

In an imperative programming language, functions do not behave like math functions.

However, in a pure functional language, a function can only read what is provided in its arguments, and the only way it can affect the world is to return values.

However, the best way to understand monads is to start using them.

You need to score on monadic laws, category theory, and just start writing code.

You must look at the monad from two sides: inside and outside.

From the inside, we can perform some actions specific to this monad.

And from the outside — we can “launch” it, “print” it, convert it to some non-monadic type.

List<String> sortedOrderList = Arrays.

asList(“order-001”, “order-005”, “order-002”, “order-003”).





toList());Do you know the so called “callback hell”?.It is a term used by developers, who are looking for a way to mask the fact that they can’t structure their code properly.

Why use callback in Java?.In order to somehow in the future, when the calculation is over, perform the following calculation.

So we need a system of types.

In Java, first we will start to make classes or interfaces, if we find ourselves closer to the architecture.

We need a type that will be the result of the calculation in the future.

Accordingly, in Java there is a Future class, which determines the result of the calculation in the future.

It has a primitive API.

You can ask it whether the calculation is completed or for the result of this calculation.

It can be blocked if the calculation is not ready yet.

So we have a Type, however to use it is rather inconvenient and complicated.

Try and do the best.

Use a monad.

Monads have different types.

What is the reason for that?.The bind function can simply take the result from one step and pass it to the next step.

Is it all?.Is it the best thing a monad could do? … It is in fact not very helpful.

Here comes the key point to understand: every useful monad does something else in addition to just being a monad.

Every useful monad has a “special power” that makes it unique.

That is, monads are useless if nothing is done with them.

For example, on Haskell they are too lazy that they do not do any calculations.

We want to make calculations in order to implement business logic.

To help with this, you can use monads as a tool.

Therefore, it is important for us to be able to convey the behavior of the program.

Let’s say the definition of some function that we want to run is inside a matrix chain of monads.

In other words, we want to work with the values that are inside the monads.

Monads are the type that allows you to wrap things up and return functions on things that are wrapped in this monad, just like a shawarma.

To ConcludeAs for the understanding of monads, do not worry about it too much.

Read about them that you find interesting, and do not worry if you do not immediately understand.

Monads are one of those things where the understanding catches you with practice, one day you just realize that you understand them.

The only way to understand monads is to write a bunch of combine libraries and to notice the resulting duplication.

After that discover that monads allow you to play this duplication.

Opening it, one creates a certain intuition for what is a monad … but this intuition — it is not something with which you can connect with someone else — I think everyone has to go through the same experience on monads generalize from what specific examples of library combines.


comRoman Taluyev.

. More details

Leave a Reply