前序。
一些改变,经过近期的学习,发现一件事情,通过搜索引擎获取内容时,往往存在大量的垃圾信息。后续做一些尝试。
- 直接上官网,看文档。
- 尝试在bilibli 中搜视频,
- 购买课程 ,进行体系学习。
当面对一个新的知识点时,用 whw 来拆解。 一 what 's 什么是xxx how to use 如何最简单的使用 whom,why 为什么 要这样,什么场景下合适。
官网学习。
1 是啥。
Vavr leveraged lambdas to create various new features based on functional patterns. One of them is a functional collection library that is intended to be a replacement for Java’s standard collections.
能做点啥
/*
1.2. Functional Programming
1.2.1. Side-Effects
1.2.2. Referential Transparency
1.2.3. Thinking in Values
1.3. Data Structures in a Nutshell
1.3.1. Mutable Data Structures
1.3.2. Immutable Data Structures
1.3.3. Persistent Data Structures
1.4. Functional Data Structures
1.4.1. Linked List
1.4.2. Queue
1.4.3. Sorted Set
1.5. State of the Collections
1.5.1. Seq
1.5.2. Set and Map
*/
2how to use (Getting started)
2.1 先maven 引入。
<dependencies>
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>0.10.4</version>
</dependency>
</dependencies>
2.2 第一个例子,创建一个Tuple (java 中其实没有元组)
//创建一个元组。
Tuple2<String, Integer> java8 = Tuple.of("Java", 8);
String s = java8._1;
Integer i = java8._2;
// (vavr, 1) 通过map 生成另一个元组。
Tuple2<String, Integer> that2 = that.map(
(s, i) -> Tuple.of(s.substring(2) + "vr", i / 8)
);
System.out.println(that2._1);
System.out.println(that2._2);
2.3 函数。
Function1<Integer, Integer> plusOne = a -> a + 1;
Function1<Integer, Integer> multiplyByTwo = a -> a * 2;
//串起来用。
Function1<Integer, Integer> add1AndMultiplyBy2 = plusOne.andThen(multiplyByTwo);
then(add1AndMultiplyBy2.apply(2)).isEqualTo(6);
//增强除法。
Function2<Integer, Integer, Integer> divide = (a, b) -> a / b;
Function2<Integer, Integer, Option<Integer>> safeDivide = Function2.lift(divide);
Option<Integer> i1 = safeDivide.apply(1,0);
Option<Integer> i2 = safeDivide.apply(2,1);
System.out.println(i1);
System.out.println(i2);
将方法增强。 (扛住异常)
...
Function2<Integer, Integer, Option<Integer>> sum = Function2.lift(this::sum);
Option<Integer> optionalResult = sum.apply(-1, 2);
System.out.println(optionalResult);
}
int sum(int first, int second) {
if (first < 0 || second < 0) {
throw new IllegalArgumentException("Only positive integers are allowed");
}
return first + second;
}
局部使用,add2 固定一个参数。add6 固定左边3个参数。
Function2<Integer, Integer, Integer> sum = (a, b) -> a + b;
Function1<Integer, Integer> add2 = sum.apply(2);
then(add2.apply(4)).isEqualTo(6);
Function5<Integer, Integer, Integer, Integer, Integer, Integer> sum = (a, b, c, d, e) -> a + b + c + d + e;
Function2<Integer, Integer, Integer> add6 = sum.apply(2, 3, 1);
then(add6.apply(4, 3)).isEqualTo(13);
缓存返回。
Function0<Double> getCode = Function0.of(Math::random).memoized();
double t1 = getCode.apply();
double t2 = getCode.apply();
System.out.println(t1);
System.out.println(t2);
2.4 Values
Option
//传统写法/
Optional<String> maybeFoo = Optional.of("foo");
then(maybeFoo.get()).isEqualTo("foo");
Optional<String> maybeFooBar = maybeFoo.map(s -> (String)null)
.map(s -> s.toUpperCase() + "bar");
then(maybeFooBar.isPresent()).isFalse();
彻底晕掉了,后面再补。