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

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.

主站蜘蛛池模板: 赣榆县| 光泽县| 榆树市| 固始县| 垫江县| 拉萨市| 连南| 堆龙德庆县| 凯里市| 临朐县| 兰坪| 梁平县| 容城县| 株洲县| 常州市| 紫阳县| 武义县| 延边| 聊城市| 泾阳县| 平潭县| 堆龙德庆县| 成武县| 方山县| 苍梧县| 太保市| 奉新县| 天全县| 额敏县| 夏津县| 卫辉市| 泌阳县| 汉川市| 淮滨县| 蓬莱市| 荥阳市| 玉环县| 吴桥县| 夹江县| 玉溪市| 成都市|