Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template partitionIt*[T](s: openArray[T], pred: untyped): tuple[t: seq[T], f: seq[T]] =
- ## 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:
- import sequtils
- 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[T]()
- f = newSeq[T]()
- for it {.inject.} in items(s):
- if pred:
- t &= it
- else:
- f &= it
- (t: t, f: f)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement