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

How it works...

The Sequence-based implementation is going to give us the following output:

Applying map function for a
Applying map function for b
[a, b]

As you can see, replacing the Collection data structure with the Sequence type allows us to gain the desired optimization.

The scenario considered in this recipe was implemented identically—first, using List, then using the Sequence type. However, we can notice the difference in the behavior of the Sequence data structure compared to that of Collection. The map() function was applied only to the first two elements of the sequence, even though the take() function was called after the mapping transformation declaration. It's also worth noting that in the example using Collection, the mapping was performed instantly when the map() function was invoked. In the case of Sequence, mapping was performed at the time of the evaluation of its elements while printing them to the console, and more precisely while converting Sequence to the List type with the following line of code:

println(transformedSequence.take(2).toList())
主站蜘蛛池模板: 秀山| 砚山县| 尼木县| 贡嘎县| 广德县| 静宁县| 特克斯县| 平潭县| 长武县| 北宁市| 衡山县| 陇川县| 得荣县| 建水县| 历史| 高淳县| 商城县| 湖南省| 大足县| 稻城县| 城口县| 望江县| 长宁区| 多伦县| 阳原县| 渑池县| 永春县| 烟台市| 麻栗坡县| 长沙市| 昔阳县| 陇南市| 岗巴县| 铜梁县| 明星| 南乐县| 家居| 潍坊市| 兴安县| 喀喇沁旗| 嘉兴市|