For循環和While循環之流的終結
本文轉載自公眾號“讀芯術”(ID:AI_Discovery)
循環語句是編程的基本組成部分。列表中的每一項都有用處,讀取輸入,直到輸入結束,在屏幕上放置n個輸入框。每當看到PR中的代碼添加了循環語句,我都怒不可遏。現在我必定仔細檢查代碼,確保循環可以終止。
我希望所有運行良好的語句庫中都看不到循環語句的蹤影,但仍然有一些悄悄混進來,所以我想告訴大家如何消除循環語句。
讓循環語句終結的關鍵是函數式編程。只需提供要在循環中執行的代碼以及循環的參數(需要循環的內容)即可。我用Java作示范語言,但其實許多語言都支持這種類型的函數式編程,這種編程可以消除代碼中的循環。
最簡單的情況是對列表中的每個元素執行操作。
- List<Integer> list = List.of(1, 2, 3);
- // bare for loop.
- for(int i : list) {
- System.out.println("int = "+ i);
- }// controlled for each
- list.forEach(i -> System.out.println("int = " + i));
在這種最簡單的情況下,無論哪種方法都沒有太大優勢。但第二種方法可以不使用bare for循環,而且語法更簡潔。
我覺得forEach語句也有問題,應該只應用于副作用安全的方法。我所說的安全副作用是指不改變程序狀態。上例只是記錄日志,因此使用無礙。其他有關安全副作用的示例是寫入文件、數據庫或消息隊列。
不安全的副作用會更改程序狀態。下面為示例及其解決方法:
- // bad side-effect, the loop alters sum
- int sum = 0;
- for(int i : list) {
- sum += i;
- }
- System.out.println("sum = " + sum);// no side-effect, sum iscalculated by loop
- sum = list
- .stream()
- .mapToInt(i -> i)
- .sum();
- System.out.println("sum = " + sum);
另一個常見的例子:
- // bad side-effect, the loop alters list2
- List<Integer> list2 = new ArrayList<>();
- for(int i : list) {
- list2.add(i);
- }
- list2.forEach(i -> System.out.println("int = " + i));// no sideeffect, the second list is built by the loop
- list2 = list
- .stream()
- .collect(Collectors.toList());
- list2.forEach(i -> System.out.println("int = " + i));
當你需要處理列表項方法中的索引時就會出現問題,但可以解決,如下:
- // bare for loop with index:
- for(int i = 0; i < list.size(); i++) {
- System.out.println("item atindex "
- + i
- + " = "
- + list.get(i));
- }// controlled loop with index:
- IntStream.range(0, list.size())
- .forEach(i ->System.out.println("item at index "
- + i
- + " = "
- + list.get(i)));
老生常談的問題:讀取文件中的每一行直到文件讀取完畢如何解決?
- BufferedReader reader = new BufferedReader(
- new InputStreamReader(
- LoopElimination.class.getResourceAsStream("/testfile.txt")));
- // while loop with clumsy looking syntax
- String line;
- while((line = reader.readLine()) != null) {
- System.out.println(line);
- }reader = new BufferedReader(
- new InputStreamReader(
- LoopElimination.class.getResourceAsStream("/testfile.txt")));
- // less clumsy syntax
- reader.lines()
- .forEach(l ->System.out.println(l));
應對上述情況有一個非常簡便的lines方法,可以返回Stream類型。但是如果一個字符一個字符地讀取呢?InputStream類沒有返回Stream
- InputStream is =
- LoopElimination.class.getResourceAsStream("/testfile.txt");
- // while loop with clumsy looking syntax
- int c;
- while((c = is.read()) != -1) {
- System.out.print((char)c);
- }
- // But this is even uglier
- InputStream nis =
- LoopElimination.class.getResourceAsStream("/testfile.txt");
- // Exception handling makes functional programming ugly
- Stream.generate(() -> {
- try {
- return nis.read();
- } catch (IOException ex) {
- throw new RuntimeException("Errorreading from file", ex);
- }
- })
- .takeWhile(ch -> ch != -1)
- .forEach(ch ->System.out.print((char)(int)ch));
這種情況下while循環看起來更好。此外,Stream版本還使用了可以返回無限項目流的 generate函數,因此必須進一步檢查以確保生成過程終止,這是由于takeWhile方法的存在。
InputStream類存在問題,因為缺少peek 方法來創建可輕松轉換為Stream的Iterator。它還會拋出一個檢查過的異常,這樣函數式編程就會雜亂無章。在這種情況下可以使用while語句讓PR通過。
為了使上述問題更簡潔,可以創建一個新的IterableInputStream類型,如下:
- static class InputStreamIterable implements Iterable<Character> {
- private final InputStream is;
- public InputStreamIterable(InputStreamis) {
- this.is = is;
- }
- public Iterator<Character>iterator() {
- return newIterator<Character>() {
- public boolean hasNext() {
- try {
- // poor man's peek:
- is.mark(1);
- boolean ret = is.read() !=-1;
- is.reset();
- return ret;
- } catch (IOException ex) {
- throw new RuntimeException(
- "Error readinginput stream", ex);
- }
- }
- public Character next() {
- try {
- return (char)is.read();
- } catch (IOException ex) {
- throw new RuntimeException(
- "Error readinginput stream", ex);
- }
- }
- };
- }
- }
這樣就大大簡化了循環問題:
- // use a predefined inputstream iterator:
- InputStreamIterable it = new InputStreamIterable(
- LoopElimination.class.getResourceAsStream("/testfile.txt"));
- StreamSupport.stream(it.spliterator(), false)
- .forEach(ch -> System.out.print(ch));
如果你經常遇到此類while循環,那么你可以創建并使用一個專門的Iterable類。但如果只用一次,就不必大費周章,這只是新舊Java不兼容的一個例子。
所以,下次你在代碼中寫for 語句或 while語句的時候,可以停下來思考一下如何用forEach 或 Stream更好地完成你的代碼。