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

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.

主站蜘蛛池模板: 吴忠市| 扶沟县| 灵台县| 城市| 肇庆市| 凌源市| 清涧县| 梁山县| 文山县| 绍兴市| 东方市| 唐山市| 凉城县| 德兴市| 建水县| 枣强县| 泸水县| 宁强县| 家居| 额尔古纳市| 夏邑县| 遂溪县| 高陵县| 固镇县| 上饶县| 湖州市| 鸡西市| 灵璧县| 兰西县| 体育| 梧州市| 体育| 清新县| 萍乡市| 杭锦旗| 西安市| 栾川县| 长治县| 祥云县| 松溪县| 梁河县|