*モナド... [#u7ee1556]
まず、自分には、理解はいいから、使えればいい。
もっというと、モナドの考え方のMaybeとStateとOptionの3つの使い方がわかればそれでいい。
具体的には、
次の関数(nextAction)を実行する前に「nullでないか確認する」時に、Maybeクラスを使うようにする。
Java8では、Maybe.thenはOptional.flatMapに相当し、Maybe.Return.doNothingReturningはOptional.ofNullableに相当するとのこと。
**参考URL [#x19a6645]
http://the.igreque.info/posts/2016/04-monad-in-java.html
上記URLから抜粋すると
とりあえず下記のメソッドのインタフェースを用意するらしい
下記は説明用のインタフェース、わかりやすくしているが、コンパイルはできないらしい
interface Monad<T1> {
<T2> Monad<T2> then(Function<T1, Monad<T2>> action);
interface Return {
<T> Monad<T> doNothingReturning(T value);
}
}
**Maybe [#l8bc833e]
class Maybe<T1> implements Monad<T1> {
private final T1 x;
Maybe(T1 x){
this.x = x;
}
public <T2> Maybe<T2> then(Function<T1, Maybe<T2>> nextAction){
if (x != null){
return nextAction.apply(x);
} else {
return new Maybe<>(null);
}
}
public static class Return implements Monad.Return {
public <T> Maybe<T> doNothingReturning(T x){
return new Maybe<>(x);
}
}
}
** ちゃんとしてるらしいけど、こだわりのプログラムのサンプルコードの場所 [#jca2f7a6]
https://github.com/highj/highj
** わかりやすいサンプルコードの場所 [#gc7e3332]
https://github.com/igrep/monad-in-java-sample