tmoditer.nim 455 B

123456789101112131415161718192021222324252627282930
  1. discard """
  2. output: "XXXXX01234"
  3. """
  4. iterator modPairs(a: var array[0..4,string]): tuple[key: int, val: var string] =
  5. for i in 0..a.high:
  6. yield (i, a[i])
  7. iterator modItems*[T](a: var array[0..4,T]): var T =
  8. for i in 0..a.high:
  9. yield a[i]
  10. var
  11. arr = ["a", "b", "c", "d", "e"]
  12. for a in modItems(arr):
  13. a = "X"
  14. for a in items(arr):
  15. stdout.write(a)
  16. for i, a in modPairs(arr):
  17. a = $i
  18. for a in items(arr):
  19. stdout.write(a)
  20. echo ""