A direct implementation of withIndex