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

How it works...

As a result, we are going to get the next 20 Fibonacci numbers printed to the console:

[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181]

The additional seed parameter in the generateSequence() provides a starting value. The nextFunction() function is applied to the seed while computing the second value. Later on, it is generating each following element using its preceding value. However, in the case of the Fibonacci sequence, we have two initial values and we need a pair of preceding values in order to compute the next value. For this reason, we wrapped them in Pair type instances. Basically, we are defining a sequence of  Pair<Int, Int> type elements, and in each nextFunction() call, we are returning a new pair that holds the values updated accordingly. At the end, we just need to use the map() function to replace each Pair element with the value of its first property. As a result, we are getting an infinite sequence of integer types returning the subsequent Fibonacci numbers.

主站蜘蛛池模板: 汉寿县| 灵武市| 策勒县| 保定市| 黔东| 三台县| 乌拉特中旗| 珲春市| 增城市| 卢湾区| 广州市| 和田县| 湘西| 瑞金市| 奎屯市| 泰来县| 县级市| 奈曼旗| 进贤县| 赫章县| 视频| 合肥市| 荆州市| 丰城市| 日土县| 平果县| 岑溪市| 金堂县| 麻城市| 湖南省| 封丘县| 巩义市| 休宁县| 延津县| 山西省| 南木林县| 金堂县| 金华市| 神农架林区| 黎川县| 工布江达县|