tsequtils.nim 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. discard """
  2. file: "tsequtils.nim"
  3. output: '''Zip: [{"Field0": 1, "Field1": 2}, {"Field0": 3, "Field1": 4}, {"Field0": 5, "Field1": 6}]
  4. Filter Iterator: 3
  5. Filter Iterator: 5
  6. Filter Iterator: 7
  7. Filter: [3, 5, 7]
  8. FilterIt: [1, 3, 7]
  9. Concat: [1, 3, 5, 7, 2, 4, 6]
  10. Deduplicate: [1, 2, 3, 4, 5, 7]
  11. @[()]'''
  12. """
  13. import sequtils, marshal
  14. proc testFindWhere(item : int) : bool =
  15. if item != 1: return true
  16. var seq1: seq[int] = @[]
  17. seq1.add(1)
  18. seq1.add(3)
  19. seq1.add(5)
  20. seq1.add(7)
  21. var seq2: seq[int] = @[2, 4, 6]
  22. var final = zip(seq1, seq2)
  23. echo "Zip: ", $$(final)
  24. #Test findWhere as a iterator
  25. for itms in filter(seq1, testFindWhere):
  26. echo "Filter Iterator: ", $$(itms)
  27. #Test findWhere as a proc
  28. var fullseq: seq[int] = filter(seq1, testFindWhere)
  29. echo "Filter: ", $$(fullseq)
  30. #Test findIt as a template
  31. var finditval: seq[int] = filterIt(seq1, it!=5)
  32. echo "FilterIt: ", $$(finditval)
  33. var concatseq = concat(seq1,seq2)
  34. echo "Concat: ", $$(concatseq)
  35. var seq3 = @[1,2,3,4,5,5,5,7]
  36. var dedupseq = deduplicate(seq3)
  37. echo "Deduplicate: ", $$(dedupseq)
  38. # bug #4973
  39. type
  40. SomeObj = object
  41. OtherObj = object
  42. field: SomeObj
  43. let aSeq = @[OtherObj(field: SomeObj())]
  44. let someObjSeq = aSeq.mapIt(it.field)
  45. echo someObjSeq