//原数组的
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);
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 + "");
}
}
/**
* @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]
}
}