Skip to content

Commit 4295a13

Browse files
committed
Following current style & structure proposals
1 parent f536859 commit 4295a13

File tree

4 files changed

+64
-44
lines changed

4 files changed

+64
-44
lines changed

src/main/scala/scala/collection/next/IterableOnceExtensions.scala

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Scala (https://www.scala-lang.org)
3+
*
4+
* Copyright EPFL and Lightbend, Inc.
5+
*
6+
* Licensed under Apache License 2.0
7+
* (http://www.apache.org/licenses/LICENSE-2.0).
8+
*
9+
* See the NOTICE file distributed with this work for
10+
* additional information regarding copyright ownership.
11+
*/
12+
13+
package scala.collection
14+
package next
15+
16+
private[next] final class NextIterableOnceOpsExtensions[A, CC[_], C](
17+
private val col: IterableOnceOps[A, CC, C]
18+
) extends AnyVal {
19+
/**
20+
* Partitions this IterableOnce into a map according to a discriminator function `key`. All the values that
21+
* have the same discriminator are then transformed by the `value` function and then reduced into a
22+
* single value with the `reduce` function.
23+
*
24+
* {{{
25+
* def occurrences[A](as: IterableOnce[A]): Map[A, Int] =
26+
* as.iterator.groupMapReduce(identity)(_ => 1)(_ + _)
27+
* }}}
28+
*
29+
* @note This will force the evaluation of the Iterator.
30+
*/
31+
def groupMapReduce[K, B](key: A => K)(f: A => B)(reduce: (B, B) => B): immutable.Map[K, B] = {
32+
val m = mutable.Map.empty[K, B]
33+
col.foreach { elem =>
34+
m.updateWith(key = key(elem)) {
35+
case Some(b) => Some(reduce(b, f(elem)))
36+
case None => Some(f(elem))
37+
}
38+
}
39+
m.to(immutable.Map)
40+
}
41+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Scala (https://www.scala-lang.org)
3+
*
4+
* Copyright EPFL and Lightbend, Inc.
5+
*
6+
* Licensed under Apache License 2.0
7+
* (http://www.apache.org/licenses/LICENSE-2.0).
8+
*
9+
* See the NOTICE file distributed with this work for
10+
* additional information regarding copyright ownership.
11+
*/
12+
13+
package scala.collection
14+
15+
import scala.language.implicitConversions
16+
17+
package object next {
18+
implicit final def scalaNextSyntaxForIterableOnceOps[A, CC[_], C](
19+
col: IterableOnceOps[A, CC, C]
20+
): NextIterableOnceOpsExtensions[A, CC, C] =
21+
new NextIterableOnceOpsExtensions(col)
22+
}

src/test/scala/scala/collection/next/TestIterableOnceExtensions.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ import org.junit.Test
1717
import scala.collection.IterableOnceOps
1818
import scala.collection.generic.IsIterableOnce
1919

20-
import IterableOnceExtensions._
21-
2220
final class TestIterableOnceExtensions {
2321
import TestIterableOnceExtensions.LowerCaseString
2422

@@ -34,7 +32,7 @@ final class TestIterableOnceExtensions {
3432

3533
@Test
3634
def iterableOnceOpsGroupMapReduce(): Unit = {
37-
def occurrences[A](coll: IterableOnceOps[A, Any, Any]): Map[A, Int] =
35+
def occurrences[A, CC[_], C](coll: IterableOnceOps[A, CC, C]): Map[A, Int] =
3836
coll.groupMapReduce(identity)(_ => 1)(_ + _)
3937

4038
val xs = Seq('a', 'b', 'b', 'c', 'a', 'a', 'a', 'b')

0 commit comments

Comments
 (0)