tmapit.nim 639 B

12345678910111213141516171819202122232425262728293031323334
  1. discard """
  2. output: '''true
  3. true'''
  4. """
  5. import sequtils
  6. var x = @[1, 2, 3]
  7. # This mapIt call will run with preallocation because ``len`` is available.
  8. var y = x.mapIt($(it+10))
  9. echo y == @["11", "12", "13"]
  10. type structureWithoutLen = object
  11. a: array[5, int]
  12. iterator items(s: structureWithoutLen): int {.inline.} =
  13. yield s.a[0]
  14. yield s.a[1]
  15. yield s.a[2]
  16. yield s.a[3]
  17. yield s.a[4]
  18. var st: structureWithoutLen
  19. st.a[0] = 0
  20. st.a[1] = 1
  21. st.a[2] = 2
  22. st.a[3] = 3
  23. st.a[4] = 4
  24. # this will run without preallocating the result
  25. # since ``len`` is not available
  26. var r = st.mapIt($(it+10))
  27. echo r == @["10", "11", "12", "13", "14"]