Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template partitionIt*(s, pred: untyped): untyped =
- ## Returns a tuple that contains two sequences.
- ## The first sequence contains the items that fulfill the predicate.
- ## The second sequence contains the items that do not fulfill the predicate.
- runnableExamples:
- let
- temperatures = @[-272.15, -2.0, 24.5, 44.31, 99.9, -113.44]
- acceptable = filterIt(temperatures, it < 50 and it > -10)
- notAcceptable = filterIt(temperatures, it > 50 or it < -10)
- partitions = partitionIt(temperatures, it < 50 and it > -10)
- assert acceptable == @[-2.0, 24.5, 44.31]
- assert notAcceptable == @[-272.15, 99.9, -113.44]
- assert partitions[0] == acceptable
- assert partitions[1] == notAcceptable
- var
- t: newSeq[type(s[0])]()
- f: newSeq[type(s[0])]()
- for it {.inject.} in items(s):
- if pred:
- result[0] &= it
- else:
- result[1] &= it
- (t, f)
- ========================
- test "partitionIt":
- let
- temperatures = @[-272.15, -2.0, 24.5, 44.31, 99.9, -113.44]
- acceptable = filterIt(temperatures, it < 50 and it > -10)
- notAcceptable = filterIt(temperatures, it > 50 or it < -10)
- partitions = partitionIt(temperatures, it < 50 and it > -10)
- check acceptable == @[-2.0, 24.5, 44.31]
- check notAcceptable == @[-272.15, 99.9, -113.44]
- check partitions[0] == acceptable
- check partitions[1] == notAcceptable
- ../src/pykot.nim(531, 26) Error: expected type, but got: newSeq(Natural(0))
Advertisement
Add Comment
Please, Sign In to add comment