Functional Programming With Java 简明教程

Optionals and Monads

单子是函数式编程的一个关键概念。单子是一种设计模式,它有助于表示一个丢失的值。它允许封装一个潜在的 null 值,允许在它周围放置变换,并在存在时提取实际值。根据定义,单子是一组以下参数。

Monad is a key concept of Functional Programming. A Monad is a design pattern which helps to represent a missing value. It allows to wrap a potential null value, allows to put transformation around it and pull actual value if present. By definition, a monad is a set of following parameters.

  1. A parametrized Type − M<T>

  2. A unit Function − T −> M<T>

  3. A bind operation − M<T> bind T −> M<U> = M<U>

Key Operations

  1. Left Identity − If a function is bind on a monad of a particular value then its result will be same as if function is applied to the value.

  2. Right Identity − If a monad return method is same as monad on original value.

  3. Associativity − Functions can be applied in any order on a monad.

Optional Class

Java 8 引入了 Optional 类,它是一个单子。它提供操作等同于单子的操作。例如, return 是一个获取值并返回单子的操作。Optional.of() 获取一个参数并返回 Optional 对象。在相同的基础上, bind 是一个将函数绑定到单子以生成单子的操作。Optional.flatMap() 是对 Optional 执行操作并将结果作为 Optional 返回的方法。

Java 8 introduced Optional class which is a monad. It provides operational equivalent to a monad. For example return is a operation which takes a value and return the monad. Optional.of() takes a parameters and returns the Optional Object. On similar basis , bind is operation which binds a function to a monad to produce a monad. Optional.flatMap() is the method which performs an operation on Optional and return the result as Optional.

  1. A parametrized Type − Optional<T>

  2. A unit Function − Optional.of()

  3. A bind operation − Optional.flatMap()

Example − Left Identity

以下示例演示了如何让 Optional 类遵循左单位律。

Following example shows how Optional class obeys Left Identity rule.

import java.util.Optional;
import java.util.function.Function;

public class FunctionTester {
   public static void main(String[] args) {
      Function<Integer, Optional<Integer>> addOneToX
         = x −> Optional.of(x + 1);
      System.out.println(Optional.of(5).flatMap(addOneToX)
         .equals(addOneToX.apply(5)));
   }
}

Output

true

Example − Right Identity

以下示例演示了如何让 Optional 类遵循右单位律。

Following example shows how Optional class obeys Right Identity rule.

import java.util.Optional;

public class FunctionTester {
   public static void main(String[] args) {
      System.out.println(Optional.of(5).flatMap(Optional::of)
         .equals(Optional.of(5)));
   }
}

Output

true

Example - Associativity

以下示例演示了如何让 Optional 类遵循结合律。

Following example shows how Optional class obeys Associativity rule.

import java.util.Optional;
import java.util.function.Function;

public class FunctionTester {
   public static void main(String[] args) {
      Function<Integer, Optional<Integer>> addOneToX
         = x −> Optional.of(x + 1);
      Function<Integer, Optional<Integer>> addTwoToX
         = x −> Optional.of(x + 2);
      Function<Integer, Optional<Integer>> addThreeToX
         = x −> addOneToX.apply(x).flatMap(addTwoToX);
      Optional.of(5).flatMap(addOneToX).flatMap(addTwoToX)
         .equals(Optional.of(5).flatMap(addThreeToX));
   }
}

Output

true