replace foreach by a closure