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.