Guest User

Untitled

a guest
Oct 17th, 2018
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 1.47 KB | None | 0 0
  1. template partitionIt*(s, pred: untyped): untyped =
  2.   ## Returns a tuple that contains two sequences.
  3.   ## The first sequence contains the items that fulfill the predicate.
  4.   ## The second sequence contains the items that do not fulfill the predicate.
  5.   runnableExamples:
  6.     let
  7.       temperatures = @[-272.15, -2.0, 24.5, 44.31, 99.9, -113.44]
  8.       acceptable = filterIt(temperatures, it < 50 and it > -10)
  9.       notAcceptable = filterIt(temperatures, it > 50 or it < -10)
  10.       partitions = partitionIt(temperatures, it < 50 and it > -10)
  11.     assert acceptable == @[-2.0, 24.5, 44.31]
  12.     assert notAcceptable == @[-272.15, 99.9, -113.44]
  13.     assert partitions[0] == acceptable
  14.     assert partitions[1] == notAcceptable
  15.  
  16.   var
  17.     t: newSeq[type(s[0])]()
  18.     f: newSeq[type(s[0])]()
  19.   for it {.inject.} in items(s):
  20.     if pred:
  21.       result[0] &= it
  22.     else:
  23.       result[1] &= it
  24.   (t, f)
  25.  
  26. ========================
  27.  
  28.   test "partitionIt":
  29.     let
  30.       temperatures = @[-272.15, -2.0, 24.5, 44.31, 99.9, -113.44]
  31.       acceptable = filterIt(temperatures, it < 50 and it > -10)
  32.       notAcceptable = filterIt(temperatures, it > 50 or it < -10)
  33.       partitions = partitionIt(temperatures, it < 50 and it > -10)
  34.  
  35.     check acceptable == @[-2.0, 24.5, 44.31]
  36.     check notAcceptable == @[-272.15, 99.9, -113.44]
  37.     check partitions[0] == acceptable
  38.     check partitions[1] == notAcceptable
  39.  
  40. ../src/pykot.nim(531, 26) Error: expected type, but got: newSeq(Natural(0))
Advertisement
Add Comment
Please, Sign In to add comment