foreach implementation