[nim] Refactoring to cleaner foreach