tseq.nim 4.1 KB

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