new generic version of scan and reduce