Beautiful World of Mondas

Beautiful World of Mondas

Because Mondas remained out of the focus of other past articles. I don’t know why this happens, but I’ll try to fill this gap.

Let me start with a disclaimer. The explanation below is no way pretends to be precise or absolutely accurate from Functional Programming. Instead, I’m focusing on the clarity and simplicity of the explanation to let as many Java developers get into this beautiful world.

When I started digging into Functional Programming a few years ago, I’ve quickly discovered that there are overwhelming amounts of information, but very little of it is understandable for the average Java developer with an almost exclusively imperative background. These days the situation is slowly changing. There are a lot of articles that explain, for example, basic FP concepts and how they apply to Java. Or articles explaining how to use Java streams properly. But Mondas remain out of the focus of these articles. I don’t know why this happens, but I’ll try to fill this gap.

What Is Monad, Anyway?

The Monad is...a design pattern. As simple as that. This design pattern consists of two parts:

  • Monad is a container for some value. For every Monad, some methods allow wrap value into Monad.
  • Monad implements “Inversion of Control” for the value contained inside. To achieve this Monad provides methods that accept functions. These functions take the value of the same type as stored in Monad and return transformed value. The transformed value is wrapped into the same kind of Monad as source one.

To understand the second part of the pattern it will be convenient to look at the imaginable Monad interface:

Java

interface Monad<T> {
    <R> Monad<R> map(Function<T, R> mapper);
    <R> Monad<R> flatMap(Function<T, Monad<R>> mapper);
}

Of course, a particular Monad usually has a far more rich interface, but these two methods definitely should be present.

At first, look accepting functions instead of giving access to value is not a big difference. In fact, this enables Monad to retain full control over how and when to apply the transformation function. When you call getter you expect to get value immediately. In the case of Monad transformation can be applied immediately or not applied at all or its application can be delayed. Lack of direct access to value inside enables the monad to represent a value that is even not yet available!

Below I’ll show some examples of Monads and which problems they can address.

java tutorial functional programming java (programming lang... monad

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Java Tutorial for Beginners | Java Tutorial | Java Programming | Core Java Tutorial | Learn Java

This "Java Tutorial for Beginners" Full course will help you master all the major concepts in java such as Arrays, Polymorphism, Abstraction, Inheritance, and Collections.

The new aspect of Java with functional programming

Functional programming is a well-known concept in Javascript and Python. But, it’s a fancy thing for a java developer. Prior to Java 8, Java has been a pure OOP where objects are first-class citizens.

Functional Programming Techniques in Java with Examples

In this article, I will write about how to write code using Java’s API for functional programming. In the end, we will write our own stream API so we can understand how to implement a functional programming style in Java.

Java Functional Programming - Learn Functional Programming with Java

In this Java Functional Programming tutorial, you will learn the power of Java Functional Interfaces and how they can be used to create powerful statements and design patterns. Learn what Java has to offer when it comes to functional programming.