官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 吴忠市| 台湾省| 双柏县| 亚东县| 赣榆县| 高淳县| 青冈县| 额济纳旗| 莱芜市| 山阴县| 大港区| 多伦县| 图片| 涟源市| 长岭县| 濉溪县| 乐山市| 勃利县| 青州市| 攀枝花市| 青田县| 汝南县| 华宁县| 大厂| 莲花县| 米易县| 正宁县| 乌什县| 武强县| 长顺县| 安庆市| 翁牛特旗| 三门县| 昔阳县| 定结县| 曲麻莱县| 温泉县| 自贡市| 高邮市| 云林县| 南安市|