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

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.

主站蜘蛛池模板: 龙南县| 高雄县| 老河口市| 孟连| 高唐县| 武义县| 永年县| 浠水县| 高阳县| 大余县| 岚皋县| 和林格尔县| 张北县| 公安县| 元氏县| 神木县| 沿河| 广汉市| 万州区| 独山县| 绍兴县| 德清县| 海阳市| 三亚市| 江达县| 轮台县| 克什克腾旗| 郯城县| 兴文县| 通山县| 邵武市| 泗洪县| 宝兴县| 瑞金市| 集贤县| 阿瓦提县| 秦皇岛市| 孟连| 沾化县| 西和县| 平顶山市|