知识卡片

普通读书笔记卡 · 743.d.6.a.1.a

归约明确累积类型与初值

理解等级:中级

sum、product等表达常见聚合;reduce逐项合并为元素类型的结果,空流没有首值,fold则需显式初始值且累积类型可与元素不同。原书把scan放在消费者列表易误读:scan维护内部状态并产出新迭代器,需继续消费才得到序列结果。

这张卡片看不懂?

已有反馈 · 0 / 10 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Chapter 1. Types Iterator Consumers · 查看原书依据

      reduce(f) Builds an accumulated value of the Item type by running a closure at each step that takes the value accumulated so far and the current item. This is a more general operation that encompasses the previous methods. fold(f) Builds an accumulated value of an arbitrary type (not just the Iterator::Item type) by running a closure at each step that takes

    参考来源

    - 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第1章《Iterator Consumers》(spine5;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch01.html;u050-ch01-077)。 - 结论依据:原文在“reduce(f)”附近的定义、例子或代码支撑本卡核心说明:sum、product等表达常见聚合;reduce逐项合并为元素类型的结果,空流没有首值,fold则需显式初始值且累积类型可与元素不同。边界按已读前提解释;示例未执行。 - 原始内容:reduce(f) Builds an accumulated value of the Item type by running a closure at each step that takes the value accumulated so far and the current item. This is a more general operation that encompasses the previous methods. fold(f) Builds an accumulated value of an arbitrary type (not just the Iterator::Item type) by running a closure at each step that takes