我们假设fn scan(int, int) -> int
.
使用时
fn count(x: int, y: int) -> int { scan(x - 1, y - 1) + scan(x - 1, y) + scan(x - 1, y + 1) + scan(x, y - 1) + scan(x, y + 1) + scan(x + 1, y - 1) + scan(x + 1, y) + scan(x + 1, y + 1) }
我得到了正确的结果.我想通过获得相同的结果fold
荷兰国际集团将scan
在给定值范围功能; 但是,我似乎无法做对.我目前的尝试是
fn count_fold(x: int, y: int) -> int { std::iter::range_inclusive(-1, 1).zip(std::iter::range_inclusive(-1, 1)).fold(0, |a, (i, j)| { a + scan(x + i, y + j) }) }
这似乎只返回正确结果的一个子集.我究竟做错了什么?TIA.