tseq.nim 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. discard """
  2. matrix: "--mm:refc; --mm:orc"
  3. output: '''
  4. Hithere, what's your name?Hathere, what's your name?
  5. fA13msg1falsefB14msg2truefC15msg3false
  6. Zip: [{"Field0": 1, "Field1": 2}, {"Field0": 3, "Field1": 4}, {"Field0": 5, "Field1": 6}]
  7. Filter Iterator: 3
  8. Filter Iterator: 5
  9. Filter Iterator: 7
  10. Filter: [3, 5, 7]
  11. FilterIt: [1, 3, 7]
  12. Concat: [1, 3, 5, 7, 2, 4, 6]
  13. Deduplicate: [1, 2, 3, 4, 5, 7]
  14. @[()]
  15. 2345623456
  16. '''
  17. """
  18. block tseq2:
  19. proc `*`(a, b: seq[int]): seq[int] =
  20. # allocate a new sequence:
  21. newSeq(result, len(a))
  22. # multiply two int sequences:
  23. for i in 0..len(a)-1: result[i] = a[i] * b[i]
  24. doAssert(@[1, 2, 3] * @[1, 2, 3] == @[1, 4, 9])
  25. block tseqcon:
  26. const nestedFixed = true
  27. type
  28. TRec {.final.} = object
  29. x, y: int
  30. s: string
  31. seq: seq[string]
  32. TRecSeq = seq[TRec]
  33. proc test() =
  34. var s, b: seq[string]
  35. s = @[]
  36. add(s, "Hi")
  37. add(s, "there, ")
  38. add(s, "what's your name?")
  39. b = s # deep copying here!
  40. b[0][1] = 'a'
  41. for i in 0 .. len(s)-1:
  42. write(stdout, s[i])
  43. for i in 0 .. len(b)-1:
  44. write(stdout, b[i])
  45. when nestedFixed:
  46. proc nested() =
  47. var
  48. s: seq[seq[string]]
  49. for i in 0..10_000: # test if the garbage collector
  50. # now works with sequences
  51. s = @[
  52. @["A", "B", "C", "D"],
  53. @["E", "F", "G", "H"],
  54. @["I", "J", "K", "L"],
  55. @["M", "N", "O", "P"]]
  56. test()
  57. when nestedFixed:
  58. nested()
  59. echo ""
  60. import os
  61. block tseqcon2:
  62. proc rec_dir(dir: string): seq[string] =
  63. result = @[]
  64. for kind, path in walk_dir(dir):
  65. if kind == pcDir:
  66. add(result, rec_dir(path))
  67. else:
  68. add(result, path)
  69. block tseqtuple:
  70. type
  71. TMsg = tuple[
  72. file: string,
  73. line: int,
  74. msg: string,
  75. err: bool]
  76. var s: seq[TMsg] = @[]
  77. s.add(("fA", 13, "msg1", false))
  78. s.add(("fB", 14, "msg2", true))
  79. s.add(("fC", 15, "msg3", false))
  80. for file, line, msg, err in items(s):
  81. stdout.write(file)
  82. stdout.write($line)
  83. stdout.write(msg)
  84. stdout.write($err)
  85. echo ""
  86. import sequtils, marshal
  87. block tsequtils:
  88. proc testFindWhere(item : int) : bool =
  89. if item != 1: return true
  90. var seq1: seq[int] = @[]
  91. seq1.add(1)
  92. seq1.add(3)
  93. seq1.add(5)
  94. seq1.add(7)
  95. var seq2: seq[int] = @[2, 4, 6]
  96. var final = zip(seq1, seq2)
  97. echo "Zip: ", $$(final)
  98. #Test findWhere as a iterator
  99. for itms in filter(seq1, testFindWhere):
  100. echo "Filter Iterator: ", $$(itms)
  101. #Test findWhere as a proc
  102. var fullseq: seq[int] = filter(seq1, testFindWhere)
  103. echo "Filter: ", $$(fullseq)
  104. #Test findIt as a template
  105. var finditval: seq[int] = filterIt(seq1, it!=5)
  106. echo "FilterIt: ", $$(finditval)
  107. var concatseq = concat(seq1,seq2)
  108. echo "Concat: ", $$(concatseq)
  109. var seq3 = @[1,2,3,4,5,5,5,7]
  110. var dedupseq = deduplicate(seq3)
  111. echo "Deduplicate: ", $$(dedupseq)
  112. # bug #4973
  113. type
  114. SomeObj = object
  115. OtherObj = object
  116. field: SomeObj
  117. let aSeq = @[OtherObj(field: SomeObj())]
  118. let someObjSeq = aSeq.mapIt(it.field)
  119. echo someObjSeq
  120. when not defined(nimseqsv2):
  121. block tshallowseq:
  122. proc xxx() =
  123. var x: seq[int] = @[1, 2, 3]
  124. var y: seq[int]
  125. system.shallowCopy(y, x)
  126. y[1] = 42
  127. doAssert y == @[1, 42, 3]
  128. doAssert x == @[1, 42, 3]
  129. xxx()
  130. block tshallowemptyseq:
  131. proc test() =
  132. var nilSeq: seq[int] = @[]
  133. var emptySeq: seq[int] = newSeq[int]()
  134. block:
  135. var t = @[1,2,3]
  136. when defined(gcRefc):
  137. shallow(nilSeq)
  138. t = nilSeq
  139. doAssert t == @[]
  140. block:
  141. var t = @[1,2,3]
  142. when defined(gcRefc):
  143. shallow(emptySeq)
  144. t = emptySeq
  145. doAssert t == @[]
  146. block:
  147. var t = @[1,2,3]
  148. shallowCopy(t, nilSeq)
  149. doAssert t == @[]
  150. block:
  151. var t = @[1,2,3]
  152. shallowCopy(t, emptySeq)
  153. doAssert t == @[]
  154. test()
  155. import strutils
  156. block ttoseq:
  157. for x in toSeq(countup(2, 6)):
  158. stdout.write(x)
  159. for x in items(toSeq(countup(2, 6))):
  160. stdout.write(x)
  161. var y: typeof("a b c".split)
  162. y = "xzy"
  163. stdout.write("\n")
  164. block tseqmapitchain:
  165. doAssert @[101, 102] == [1, 2].mapIt(func (x: int): int = it + x).mapIt(it(100))
  166. for i in 0..100:
  167. # fix #14655
  168. var test = newSeqOfCap[uint32](1)
  169. test.setLen(1)
  170. doAssert test[0] == 0, $(test[0], i)