- 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.
- SQL Server 從入門到項目實踐(超值版)
- 手機安全和可信應用開發指南:TrustZone與OP-TEE技術詳解
- The Supervised Learning Workshop
- Kibana Essentials
- 企業級Java EE架構設計精深實踐
- C#程序設計實訓指導書
- 高級C/C++編譯技術(典藏版)
- PHP+MySQL+Dreamweaver動態網站開發實例教程
- 微信小程序開發解析
- 計算機應用基礎實踐教程
- Mastering Android Game Development
- 編程與類型系統
- Unity 2018 Shaders and Effects Cookbook
- Tableau Desktop可視化高級應用
- SEO教程:搜索引擎優化入門與進階(第3版)