使用Vavr进行函数式编程(一)

1. 元组 ( Tuple )

  • 可以放入多个不同参数的数据类型。弥补Java的函数只能返回一个值的缺陷(可以理解为就是一个特殊对象)。
  • 不易放入多个变量,会导致代码不易阅读。
  • demo
      //原数组的
      Tuple2<String, Integer> tuple2 = Tuple.of("Hello", 100);
      //对Tuple2内部变量进行操作
      Tuple2<String, Integer> updatedTuple2 = tuple2.map(String::toUpperCase, v -> v * 5);
      //进行Tuple2转换为某个特定的变量
      String result = updatedTuple2.apply((str, number) -> String.join(", ",
              str, number.toString()));
      System.out.println(result);

2. 函数 ( Function )

  • Java 8 中只提供了接受一个参数的 Function 和接受 2 个参数的 BiFunction。
  • Vavr 提供了函数式接口 Function0、Function1 到 Function8,可以描述最多接受 8 个参数的函数。
  • 但是比较难受的是这些接口的方法 apply 不能抛出异常。如果需要抛出异常,可以使用对应的接口 CheckedFunction0、CheckedFunction1 到 CheckedFunction8。
  • demo如下
package com.yuanxindong.fp.vavr.data;

import io.vavr.Function3;
import io.vavr.Tuple;
import io.vavr.Tuple3;

import java.util.function.Function;

/**
 * @author yuanxindong
 * @date 2020/11/17 下午11:22
 */
public class FunctionDemo {

  /**
   * 使用普通Java8函数function
   *
   * @param valueToBeOperated
   * @param function
   */
  static void modifyTheValue(int valueToBeOperated, Function<Integer, Integer> function) {
    int newValue = function.apply(valueToBeOperated);
    System.out.println(newValue);
  }

  /**
   * 使用vavr函数
   *
   * @param valueToBeOperated tuple的方式传递函数
   * @param function 函数
   */
  static void modifyTheValue2(
      Tuple3<Integer, Integer, Integer> valueToBeOperated,
      Function3<Integer, Integer, Integer, String> function) {

    // 将tuple中的参数放入
    String newValue =
        function.apply(valueToBeOperated._1, valueToBeOperated._2, valueToBeOperated._3);
    System.out.println(newValue);
  }

  public static void main(String[] args) {

    int incr = 20;
    int myNumber = 10;

    // 通过传递函数进行操作这个MyNumber这个值
    modifyTheValue(myNumber, val -> val + incr);

    Tuple3<Integer, Integer, Integer> value = Tuple.of(1, 2, 3);

    // 使用vavr函数进行计算 val1 + val2 + val3的字符传
    modifyTheValue2(value, (val1, val2, val3) -> val1 + val2 + val3 + "");
  }
}

  1. 组合
  • 函数的组合指的是用一个函数的执行结果作为参数,来调用另外一个函数所得到的新函数。
  • demo
/**
 * @author yuanxindong
 * @date 2020/11/18 上午12:08
 */
public class FunctionCombinationDemo {
  public static void main(String[] args) {
    // 定义函数
    Function3<Integer, Integer, Integer, Integer> function3 = (v1, v2, v3) -> (v1 + v2) * v3;

    // 使用andThen对函数中的值进行操作
    Function3<Integer, Integer, Integer, Integer> composed = function3.andThen(v -> v * 100);

    // 使用apply进行函数包装
    int result = composed.apply(1, 2, 3);
    // 输出结果 900
    System.out.println(result);

    //另一种表达方式
    Function1<String, String> function1 = String::toUpperCase;

    //先通过函数将对象转换为字符串,在通过函数将这个数据进行再次操作
    Function1<Object, String> toUpperCase = function1.compose(Object::toString);
    //输入数组
    String str = toUpperCase.apply(List.of("a", "b"));
    System.out.println(str);
    // 输出结果[A, B]
  }
}

  • 从中我们可以看到andThen和compose的区别在哪里吗?前者是先执行function函数,在执行function里面的函数。后者反之。

猜你喜欢

转载自blog.csdn.net/weixin_40413961/article/details/109759911