implement closure for index counting