Skip to content

Commit 2d0db9a

Browse files
authored
Merge pull request #303 from yannbolliger/master
Add partitionMap to 2.11 and 2.12
2 parents 1f591c4 + 96c7b83 commit 2d0db9a

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ class IteratorExtensionMethods[A](private val self: c.Iterator[A]) extends AnyVa
233233
self.sameElements(that.iterator)
234234
}
235235
def concat[B >: A](that: c.TraversableOnce[B]): c.TraversableOnce[B] = self ++ that
236-
def tapEach[U](f: A => U): c.Iterator[A] = self.map(a => { f(a); a })
236+
def tapEach[U](f: A => U): c.Iterator[A] = self.map(a => { f(a); a })
237237
}
238238

239239
class TraversableOnceExtensionMethods[A](private val self: c.TraversableOnce[A]) extends AnyVal {
@@ -319,6 +319,21 @@ class TraversableLikeExtensionMethods[A, Repr](private val self: c.GenTraversabl
319319
def tapEach[U](f: A => U)(implicit bf: CanBuildFrom[Repr, A, Repr]): Repr =
320320
self.map(a => { f(a); a })
321321

322+
def partitionMap[A1, A2, That, Repr1, Repr2](f: A => Either[A1, A2])(
323+
implicit bf1: CanBuildFrom[Repr, A1, Repr1],
324+
bf2: CanBuildFrom[Repr, A2, Repr2]
325+
): (Repr1, Repr2) = {
326+
val l = bf1()
327+
val r = bf2()
328+
self.foreach { x =>
329+
f(x) match {
330+
case Left(x1) => l += x1
331+
case Right(x2) => r += x2
332+
}
333+
}
334+
(l.result(), r.result())
335+
}
336+
322337
def groupMap[K, B, That](key: A => K)(f: A => B)(
323338
implicit bf: CanBuildFrom[Repr, B, That]): Map[K, That] = {
324339
val map = m.Map.empty[K, m.Builder[B, That]]
@@ -338,7 +353,7 @@ class TraversableLikeExtensionMethods[A, Repr](private val self: c.GenTraversabl
338353
val k = key(elem)
339354
val v = map.get(k) match {
340355
case Some(b) => reduce(b, f(elem))
341-
case None => f(elem)
356+
case None => f(elem)
342357
}
343358
map.put(k, v)
344359
}

compat/src/test/scala/test/scala/collection/CollectionTest.scala

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ class CollectionTest {
3636
val bT: BitSet = b
3737
assertEquals(BitSet(1, 2, 3), b)
3838

39-
val c = xs.to(PriorityQueue)
40-
val cT: PriorityQueue[Int] = c
39+
val c = xs.to(PriorityQueue)
40+
val cT: PriorityQueue[Int] = c
4141
assert(PriorityQueue(1, 2, 3) sameElements c)
4242

4343
val ys = List(1 -> "a", 2 -> "b")
@@ -48,8 +48,8 @@ class CollectionTest {
4848
assertTrue(m.isInstanceOf[Map[_, _]])
4949

5050
// Stream.to(Seq) doesn't evaluate the stream
51-
val strm = 1 #:: {throw new Exception("not lazy")} #:: Stream.empty[Int]
52-
val strmsq: Seq[Int] = strm.to(Seq)
51+
val strm = 1 #:: { throw new Exception("not lazy") } #:: Stream.empty[Int]
52+
val strmsq: Seq[Int] = strm.to(Seq)
5353
var strmln: LinearSeq[Int] = strm.to(LinearSeq)
5454
}
5555

@@ -101,10 +101,23 @@ class CollectionTest {
101101
assertEquals(Map(3 -> 3, 4 -> 1), res)
102102
}
103103

104+
@Test
105+
def partitionMapTest(): Unit = {
106+
val empty = Seq.empty[Int].partitionMap(Right(_))
107+
assertEquals((Seq(), Seq()), empty)
108+
109+
val res = Seq("foo", "test", "bar", "baz")
110+
.partitionMap {
111+
case s if s.contains("a") => Left(s)
112+
case s => Right(s.length)
113+
}
114+
assertEquals((Seq("bar", "baz"), Seq("foo".length, "test".length)), res)
115+
}
116+
104117
@Test
105118
def tapEach(): Unit = {
106119
var count = 0
107-
val it = Iterator(1, 2, 3).tapEach(count += _)
120+
val it = Iterator(1, 2, 3).tapEach(count += _)
108121
assertEquals(0, count)
109122
it.foreach(_ => ())
110123
assertEquals(6, count)

0 commit comments

Comments
 (0)