- 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.
- 手機安全和可信應用開發指南:TrustZone與OP-TEE技術詳解
- 程序員面試筆試寶典(第3版)
- C程序設計簡明教程(第二版)
- Protocol-Oriented Programming with Swift
- 硅谷Python工程師面試指南:數據結構、算法與系統設計
- Unity 2017 Mobile Game Development
- Scratch趣味編程:陪孩子像搭積木一樣學編程
- SQL Server 2008 R2數據庫技術及應用(第3版)
- Everyday Data Structures
- Java 從入門到項目實踐(超值版)
- 深度實踐KVM:核心技術、管理運維、性能優化與項目實施
- 程序員的成長課
- Implementing Microsoft Dynamics NAV(Third Edition)
- Learning WordPress REST API
- 前端程序員面試算法寶典