This time, I will write about functional programming from an object-oriented perspective.
There is a lot of controversy about this, but I find it accurate to guess the translation "object" for objects.
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.
To be precise, it should be a pointer and stores the instance at the point.
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?
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.
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.
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