超越Java8:Stream的延遲計算
文章目錄
- 超越Java8:Stream的延遲計算
- 一、函式式編程
- 1.1 示例一:方法中沒有任何操作會修改現有結構
- 1.2 實體二:“尾-遞”迭代
- 方案一:迭代
- 方案二:使用遞回
- 方案三:使用Stream
- 方案四:遞回的優化:“尾-遞”迭代
- 二、科里化
- 三、函式式資料結構——持久化的
- 四、Stream的延遲計算
- 4.1 串列介面
- 4.2 延遲串列
- 4.3 創建一個無限延遲的串列
- 4.4 創建一個無限延遲的質數串列
- 4.5 使用無限延遲的質數串列
一、函式式編程
-
要被稱為函式式,函式或方法不應該拋出任何例外,使用
Optional<R>型別作為回傳值, -
透明性:方法中沒有任何操作會修改現有結構
-
使用Java8進行編程時,盡量使用Stream取代迭代操作,如果遞回可以更簡潔,且不帶副作用,應該使用遞回替換迭代,
"尾-遞"迭代,不需要在不同的堆疊楨上保存每次遞回計算的中間值,目前Java不支持這種優化,很多的現代JVM語言,比如Scala和Groovy都支持這種形式遞回迭代的優化,
1.1 示例一:方法中沒有任何操作會修改現有結構
獲取串列的子集:
public static List<List<Integer>> findAllSubList(List<Integer> list) {
if (list.size()==0) {
List<List<Integer>> res = new ArrayList<>();
res.add(Collections.emptyList());
return res;
}
Integer first = list.get(0);
List<Integer> subList = list.subList(1, list.size());
List<List<Integer>> allSubList = findAllSubList(subList);
List<List<Integer>> allSubList2 = insertAll(first, allSubList);
return concat(allSubList, allSubList2);
}
private static List<List<Integer>> concat(List<List<Integer>> allSubList, List<List<Integer>> allSubList2) {
List<List<Integer>> res = new ArrayList<>(allSubList);
res.addAll(allSubList2);
return res;
}
private static List<List<Integer>> insertAll(Integer item, List<List<Integer>> allSubList) {
List<List<Integer>> res = new ArrayList<>();
for (List<Integer> a : allSubList) {
List<Integer> oneList = new ArrayList<>(a);
oneList.add(item);
res.add(oneList);
}
return res;
}
1.2 實體二:“尾-遞”迭代
求n的階乘:
方案一:迭代
/**
* 使用迭代計算階乘
* r 和 i 在每輪迭代中都會更新
* @param n
* @return
*/
public static int factorialIterator(int n) {
int r = 1;
for (int i=1; i<=n; i++) {
r *= i;
}
return r;
}
方案二:使用遞回
/**
* 使用遞回 計算階乘
* 比迭代都效率差:因為每次遞回都需要創建堆疊楨
* @param n
* @return
*/
public static int factorialRecursive(int n) {
return n==1? 1 : n * factorialIterator(n-1);
}
方案三:使用Stream
/**
* 使用Stream 計算階乘
* @param n
* @return
*/
public static int factorialStream(int n) {
return IntStream.rangeClosed(1, n).reduce(1, (x, y)->x*y);
}
方案四:遞回的優化:“尾-遞”迭代
/**
* 尾-遞 迭代
* @param n
* @return
*/
public static int factorialTailIterator(int n) {
return factorialTailHelp(1, n);
}
/**
* 尾-遞 迭代遞幫助類
* @param acc
* @param n
* @return
*/
private static int factorialTailHelp(int acc, int n) {
return n==1?acc:factorialTailHelp(acc*n, n-1);
}
二、科里化
科里化:幫助你模塊化函式,提高代碼重用性的技術,
科里化表示一種將一個帶有n元組引數的函式轉換成n個一元函式鏈的方法,
三、函式式資料結構——持久化的
資料結構的值始終保持一致,不受其他部分變化的影響,
附加條件:所有使用持久化資料結構的用戶都必須遵守這一“不修改“原則,不對回傳值就行修改,
四、Stream的延遲計算
創建一個質數串列:
4.1 串列介面
/**
* @Date 2021/9/5
* @Author lifei
*/
public interface MyList<T> {
T head();
MyList<T> tail();
MyList<T> filter(Predicate<T> p);
default boolean isEmpty() {
return true;
}
}
4.2 延遲串列
public class LazyList<T> implements MyList<T> {
final T head;
final Supplier<MyList<T>> tail;
public LazyList(T head, Supplier<MyList<T>> tail) {
this.head = head;
this.tail = tail;
}
@Override
public T head() {
return head;
}
@Override
public MyList<T> tail() {
return tail.get();
}
@Override
public MyList<T> filter(Predicate<T> p) {
return isEmpty()?this:p.test(head())? new LazyList<>(head, ()->tail().filter(p)):tail().filter(p);
}
@Override
public boolean isEmpty() {
return false;
}
}
4.3 創建一個無限延遲的串列
/**
* 創建一個無限延遲的串列
* @param n
* @return
*/
public static LazyList<Integer> from(int n) {
return new LazyList<>(n, ()->from(n+1));
}
4.4 創建一個無限延遲的質數串列
/**
* 創建一個無限回圈的 質數串列
* @param numbers
* @return
*/
public static MyList<Integer> primes(MyList<Integer> numbers) {
return new LazyList<>(numbers.head(), ()->primes(numbers.tail().filter(n->n%numbers.head()!=0)));
}
4.5 使用無限延遲的質數串列
public static void main(String[] args) {
LazyList<Integer> numbers = from(2);
Integer res2 = numbers.head();
Integer res3 = numbers.tail().head();
Integer res4 = numbers.tail().tail().head();
System.out.println(res2);
System.out.println(res3);
System.out.println(res4);
System.out.println("創建一個無限延遲的質數串列");
MyList<Integer> primes = primes(numbers);
for (int i=0; i<30; i++) {
if (!primes.isEmpty()){
System.out.print(primes.head() + ", ");
primes = primes.tail();
}
}
System.out.println();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/298074.html
標籤:其他
上一篇:Flutter是跨平臺開發終極之選嗎?Android開發該如何快速上手Flutter?
下一篇:補一篇墨跡了好久的閱讀小結
