replace `range` with an external iterator