foreach algorithms