Alternative implementation of for-each