- Scala Functional Programming Patterns
- Atul S. Khot
- 186字
- 2021-07-30 09:44:25
Chapter 3. Recursion and Chasing your Own Tail
In the first chapter, we wrote a nested method, groupThem()
, that called itself. In the second chapter, the BST traversal method is similar to the nested method. We saw that the method calls itself to traverse the children. A method calling itself is recursion. In the previous chapter, we also touched upon tail recursion and Scala's @tailrec
annotation.
Let's look at these concepts more closely and see how all these help us write succinct code, and how going recursive promotes immutability. In this chapter, we will first look at recursive structures—a structure is recursive if the shape of the whole recurs in the shape of the parts. We will then look at how Scala's pattern matching helps us to work on the composing parts. Next, we will take a look at a possible problem with very large structures and the mechanism to deal with them—namely tail call optimization (TCO) and @tailrec
annotations. Finally, we will get a handle on persistent data structures and structural sharing to avoid needless copying while preserving immutability.
- 構建移動網站與APP:HTML 5移動開發入門與實戰(跨平臺移動開發叢書)
- Mastering LibGDX Game Development
- C#程序設計基礎:教程、實驗、習題
- 青少年Python編程入門
- Java Web程序設計任務教程
- 深入理解Elasticsearch(原書第3版)
- Java EE 8 Application Development
- Python爬蟲、數據分析與可視化:工具詳解與案例實戰
- UNIX Linux程序設計教程
- Bootstrap for Rails
- Windows Phone 8 Game Development
- JSP程序設計與案例實戰(慕課版)
- ASP.NET求職寶典
- C語言程序設計
- Java高手是怎樣煉成的:原理、方法與實踐