tseq.nim 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. Minmax: (1, 7)
  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. doAssert(@[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 minmax:
  122. doAssert minmax(@[0]) == (0, 0)
  123. doAssert minmax(@[0, 1]) == (0, 1)
  124. doAssert minmax(@[1, 0]) == (0, 1)
  125. doAssert minmax(@[8,2,1,7,3,9,4,0,5]) == (0, 9)
  126. echo "Minmax: ", $(minmax(concat(seq1, seq2)))
  127. when not defined(nimseqsv2):
  128. block tshallowseq:
  129. proc xxx() =
  130. var x: seq[int] = @[1, 2, 3]
  131. var y: seq[int]
  132. system.shallowCopy(y, x)
  133. y[1] = 42
  134. doAssert y == @[1, 42, 3]
  135. doAssert x == @[1, 42, 3]
  136. xxx()
  137. block tshallowemptyseq:
  138. proc test() =
  139. var nilSeq: seq[int] = @[]
  140. var emptySeq: seq[int] = newSeq[int]()
  141. block:
  142. var t = @[1,2,3]
  143. when defined(gcRefc):
  144. shallow(nilSeq)
  145. t = nilSeq
  146. doAssert t == @[]
  147. block:
  148. var t = @[1,2,3]
  149. when defined(gcRefc):
  150. shallow(emptySeq)
  151. t = emptySeq
  152. doAssert t == @[]
  153. block:
  154. var t = @[1,2,3]
  155. shallowCopy(t, nilSeq)
  156. doAssert t == @[]
  157. block:
  158. var t = @[1,2,3]
  159. shallowCopy(t, emptySeq)
  160. doAssert t == @[]
  161. test()
  162. import strutils
  163. block ttoseq:
  164. for x in toSeq(countup(2, 6)):
  165. stdout.write(x)
  166. for x in items(toSeq(countup(2, 6))):
  167. stdout.write(x)
  168. var y: typeof("a b c".split)
  169. y = "xzy"
  170. stdout.write("\n")
  171. block tseqmapitchain:
  172. doAssert @[101, 102] == [1, 2].mapIt(func (x: int): int = it + x).mapIt(it(100))
  173. for i in 0..100:
  174. # fix #14655
  175. var test = newSeqOfCap[uint32](1)
  176. test.setLen(1)
  177. doAssert test[0] == 0, $(test[0], i)
  178. # bug #22560
  179. doAssert len(newSeqOfCap[int](42)) == 0
  180. block: # bug #17197
  181. type Matrix = seq[seq[int]]
  182. proc needlemanWunsch(sequence1: string, sequence2: string, gap_penal: int8, match: int8, indel_penal: int8): bool =
  183. let seq2_len = sequence2.len
  184. var grid: Matrix
  185. for i in sequence1:
  186. grid.add(newSeqOfCap[seq[int]](seq2_len))
  187. result = true
  188. doAssert needlemanWunsch("ABC", "DEFG", 1, 2, 3)