New algorithms for foreach and sort