Problems with fold in Rust

fold, rust

Solution

When you zip two iterators, you are not creating the "product" of the iterations, like you seem to be wanting to do. Rather, you are iterating both iterators at the same time and creating a pair with the iterated values. So in the `count_fold` version, the closure will only be called with the following pairs:

(-1, -1)
(0, 0)
(1, 1)

So your `count_fold` function is actually akin to

scan(x - 1, y - 1) + scan(x, y) + scan(x - 1, y + 1)

I can be wrong, but I don't think there is a function in `std` that create the product of two iterators.

Moreover, your `count` method do not use `scan(x, y)` in the sum, so it is not even really the product of the iterators; you have to be careful about that if you want to create your own product iterator and use it for that purpose.

Problem

Let's assume `fn scan(int, int) -> int`. When using ``` 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) } ``` I get correct results. I am trying to get the same results by `fold`ing the `scan` function over the given value ranges; however, I can't seem to get it right. My current attempt is ``` 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) }) } ``` which seems to return only a subset of the correct results. What am I doing wrong? TIA.

Original source