Stateful scan function. Like reduce but for repeated calls to a function instead of elements in a list.