前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用Vavr进行函数式编程(一)

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

作者头像
袁新栋-jeff.yuan
发布2020-11-24 15:24:03
1.3K0
发布2020-11-24 15:24:03
举报

1. 元组 ( Tuple )

  • 可以放入多个不同参数的数据类型。弥补Java的函数只能返回一个值的缺陷(可以理解为就是一个特殊对象)。
  • 不易放入多个变量,会导致代码不易阅读。
  • demo
代码语言:javascript
复制
      //原数组的
      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如下
代码语言:javascript
复制
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
代码语言:javascript
复制
/**
 * @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里面的函数。后者反之。
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-11-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 元组 ( Tuple )
  • 2. 函数 ( Function )
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档