[JAVA] About the usefulness of monads from an object-oriented perspective

Let's go back from object orientation.

This time, I will write about functional programming from an object-oriented perspective.

What is an object-oriented "object"?

There is a lot of controversy about this, but I find it accurate to guess the translation "object" for objects.

An object is a value

This means that the object itself is also a value. A simple example is shown below in ** Java **.

public class Foo {
    private int value;
    Foo (v) {
        this.value = v;
    }
    public add_foo (v) {
        this.value += v;
    }
}

public class Test_Foo {
    public static void main (String[] args) {
        Foo foo = new Foo(5);
    }
}

Foo foo = new Foo(5); To explain a little roundabout, The type of the variable foo is Foo, and an instance of Foo is assigned to foo. It will be.

This instance, the value, right?

To be precise, it should be a pointer and stores the instance at the point.

Consider calling a method of values

Maybe I do it well in Java. For example, when operating a character string, The function returns the result directly, but isn't it possible to handle strings conveniently?

Add a method to a primitive value

The subject is this. Let's add a method to the value 3. How can I do it? In Java, inherit the class written in the following description and add methods, The idea is to use the added method,

public class IFT {
    private int v;
    IFT (int v) {
        this.v = v;
    }
    public int value () {
        return v;
    }
}

With javascript you can write like this.

class MonoInt {
  constructor (v) {
    this.v = v;
  }
  static of (v) {
    return new MonoInt(v);
  }
  get join () {
    return this.v;
  }
  map (f) {
    return MonoInt.of(f(this.v));
  }
  bind (f) {
    return this.map(f).join;
  }
}
MonoInt.of(3).map(v => v + 3).join; // 6
MonoInt.of(5).bind(v => Monoint.of(v * 8)).join; // 40

An arrow function, λ (lambda), which can be used from Java8 etc. has come out. At this time, MonoInt takes only one value, and writes the code on the condition that it does not refer to any value from the outside and does not add it.

Objects can be interpreted as a category

An object has a ** value ** called a property and is an implementation concept of a ** natural transformation ** called a method. And if the object itself is also a value, if you change the above code as follows ...


class Foo {
  constructor (v) {
    this.value = v;
    this.key = "foo";
  }
  get oo () {
    return this.v;
  }
  set oo (v) {
    this.v = v;
  }
  resetKey (v) {
    this.key = "foo" + v;
  }
}

class Fix {
  constructor (v) {
    this.v = v;
  }
  static of (v) {
    return new Fix(v);
  }
  get join () {
    return this.v;
  }
  map (f) {
    return Fix.of(f(this.v));
  }
  bind (f) {
    return this.map(f).join;
  }
  re (f) {
    this.map(f);
    return this;
  }
  assign (o) {
    return this.re(v => Object.assign(v, o));
  }
}

Fix.of(new Foo(5)).map(foo => foo.oo).map(v => v + 3).join
// 8

Fix.of(new Foo(5)).re(foo => foo.oo += 3).assign(["bar", "baz"])).join
/*
Foo {
  "0": "bar",
  "1": "baz",
  value: 8,
  key: "foo"
}
*/

A function that handles the object in bulk even when the method of the object returns nothing I was able to implement ** re **. This allows us to implement ** assign **, which can delegate Object.assign.

If you add a method that uses re, map, and bind internally using the Fix body or a class that inherits Fix, you can execute the method as if the object originally had it. ..

At this time, the assigned object body does not have to have that method.

Monads are not difficult

To be precise, it seems that monads that can perform endo (redo) are said to be surplus monads. It's convenient, so I'd like to incorporate it more and more.

If you have any corrections or suggestions, we will respond sincerely. Thank you for staying with us until the end.

Recommended Posts

About the usefulness of monads from an object-oriented perspective
Ruby from the perspective of other languages
Let's summarize the Java 8 grammar from the perspective of an iOS engineer
How to write Scala from the perspective of Java
Java language from the perspective of Kotlin and C #
About the handling of Null
About an instance of java
About the description of Docker-compose.yml
From the habit of creating Value Objects for object-oriented understanding
About the behavior of ruby Hash # ==
About the basics of Android development
About the role of the initialize method
Think about the 7 rules of Optional
Summary about the introduction of Device
About the log level of java.util.logging.Logger
Comparison of nullable, non-nullable, non-nullable and safe types from the perspective of null safety
Object-oriented design prescription talked about by an object-oriented uncle with 25 years of object-oriented history
About the version of Docker's Node.js image
[Challenge CircleCI from 0] Learn the basics of CircleCI
What is testing? ・ About the importance of testing
About the operation of next () and nextLine ()
Compare the elements of an array (Java)
The story of RxJava suffering from NoSuchElementException
About the initial display of Spring Framework
About the error message Invalid redeclaration of'***'
About the treatment of BigDecimal (with reflection)
About the number of threads of Completable Future
About the mechanism of the Web and HTTP
Find the difference from a multiple of 10
Has the content of useBodyEncodingForURI changed from Tomcat8?
About the speed when fetching values from HashMap
Think about the combination of Servlet and Ajax
About the official start guide of Spring Framework
About the description order of Java system properties
About the idea of anonymous classes in Java
About next () and nextLine () of the Scanner class
About two months of working from home for an engineer diagnosed with viral pneumonia.