-
Notifications
You must be signed in to change notification settings - Fork 18
Add groupMapReduce method to IterableOnceOps #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7f83f63
Adding groupMapReduce method to Iterator
BalmungSan ecf7fcc
Following structure and naming conventions
BalmungSan f536859
Adding the extension method to any IterableOnce instead to just Itera…
BalmungSan 4295a13
Following current style & structure proposals
BalmungSan 8f9fd9c
Merge branch 'main' into add-iterator-group-methods
BalmungSan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
src/main/scala/scala/collection/next/NextIterableOnceOpsExtensions.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Scala (https://www.scala-lang.org) | ||
* | ||
* Copyright EPFL and Lightbend, Inc. | ||
* | ||
* Licensed under Apache License 2.0 | ||
* (http://www.apache.org/licenses/LICENSE-2.0). | ||
* | ||
* See the NOTICE file distributed with this work for | ||
* additional information regarding copyright ownership. | ||
*/ | ||
|
||
package scala.collection | ||
package next | ||
|
||
private[next] final class NextIterableOnceOpsExtensions[A, CC[_], C]( | ||
private val col: IterableOnceOps[A, CC, C] | ||
) extends AnyVal { | ||
/** | ||
* Partitions this IterableOnce into a map according to a discriminator function `key`. All the values that | ||
* have the same discriminator are then transformed by the `value` function and then reduced into a | ||
* single value with the `reduce` function. | ||
* | ||
* {{{ | ||
* def occurrences[A](as: IterableOnce[A]): Map[A, Int] = | ||
* as.iterator.groupMapReduce(identity)(_ => 1)(_ + _) | ||
* }}} | ||
* | ||
* @note This will force the evaluation of the Iterator. | ||
*/ | ||
def groupMapReduce[K, B](key: A => K)(f: A => B)(reduce: (B, B) => B): immutable.Map[K, B] = { | ||
val m = mutable.Map.empty[K, B] | ||
col.foreach { elem => | ||
m.updateWith(key = key(elem)) { | ||
case Some(b) => Some(reduce(b, f(elem))) | ||
case None => Some(f(elem)) | ||
} | ||
} | ||
m.to(immutable.Map) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Scala (https://www.scala-lang.org) | ||
* | ||
* Copyright EPFL and Lightbend, Inc. | ||
* | ||
* Licensed under Apache License 2.0 | ||
* (http://www.apache.org/licenses/LICENSE-2.0). | ||
* | ||
* See the NOTICE file distributed with this work for | ||
* additional information regarding copyright ownership. | ||
*/ | ||
|
||
package scala.collection | ||
|
||
import scala.language.implicitConversions | ||
|
||
package object next { | ||
implicit final def scalaNextSyntaxForIterableOnceOps[A, CC[_], C]( | ||
col: IterableOnceOps[A, CC, C] | ||
): NextIterableOnceOpsExtensions[A, CC, C] = | ||
new NextIterableOnceOpsExtensions(col) | ||
} |
84 changes: 84 additions & 0 deletions
84
src/test/scala/scala/collection/next/TestIterableOnceExtensions.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Scala (https://www.scala-lang.org) | ||
* | ||
* Copyright EPFL and Lightbend, Inc. | ||
* | ||
* Licensed under Apache License 2.0 | ||
* (http://www.apache.org/licenses/LICENSE-2.0). | ||
* | ||
* See the NOTICE file distributed with this work for | ||
* additional information regarding copyright ownership. | ||
*/ | ||
|
||
package scala.collection.next | ||
|
||
import org.junit.Assert._ | ||
import org.junit.Test | ||
import scala.collection.IterableOnceOps | ||
import scala.collection.generic.IsIterableOnce | ||
|
||
final class TestIterableOnceExtensions { | ||
import TestIterableOnceExtensions.LowerCaseString | ||
|
||
@Test | ||
def iteratorGroupMapReduce(): Unit = { | ||
def occurrences[A](coll: IterableOnce[A]): Map[A, Int] = | ||
coll.iterator.groupMapReduce(identity)(_ => 1)(_ + _) | ||
|
||
val xs = Seq('a', 'b', 'b', 'c', 'a', 'a', 'a', 'b') | ||
val expected = Map('a' -> 4, 'b' -> 3, 'c' -> 1) | ||
assertEquals(expected, occurrences(xs)) | ||
} | ||
|
||
@Test | ||
def iterableOnceOpsGroupMapReduce(): Unit = { | ||
def occurrences[A, CC[_], C](coll: IterableOnceOps[A, CC, C]): Map[A, Int] = | ||
coll.groupMapReduce(identity)(_ => 1)(_ + _) | ||
|
||
val xs = Seq('a', 'b', 'b', 'c', 'a', 'a', 'a', 'b') | ||
val expected = Map('a' -> 4, 'b' -> 3, 'c' -> 1) | ||
assertEquals(expected, occurrences(xs)) | ||
} | ||
|
||
@Test | ||
def anyLikeIterableOnceGroupMapReduce(): Unit = { | ||
def occurrences[Repr](coll: Repr)(implicit it: IsIterableOnce[Repr]): Map[it.A, Int] = | ||
it(coll).iterator.groupMapReduce(identity)(_ => 1)(_ + _) | ||
|
||
val xs = "abbcaaab" | ||
val expected = Map('a' -> 4, 'b' -> 3, 'c' -> 1) | ||
assertEquals(expected, occurrences(xs)) | ||
} | ||
|
||
@Test | ||
def customIterableOnceOpsGroupMapReduce(): Unit = { | ||
def occurrences(coll: LowerCaseString): Map[Char, Int] = | ||
coll.groupMapReduce(identity)(_ => 1)(_ + _) | ||
|
||
val xs = LowerCaseString("abBcAaAb") | ||
val expected = Map('a' -> 4, 'b' -> 3, 'c' -> 1) | ||
assertEquals(expected, occurrences(xs)) | ||
} | ||
} | ||
|
||
object TestIterableOnceExtensions { | ||
final case class LowerCaseString(source: String) extends IterableOnce[Char] with IterableOnceOps[Char, Iterable, String] { | ||
override def iterator: Iterator[Char] = source.iterator.map(_.toLower) | ||
|
||
override def scanLeft[B](z: B)(op: (B, Char) => B): Iterable[B] = ??? | ||
override def filter(p: Char => Boolean): String = ??? | ||
override def filterNot(pred: Char => Boolean): String = ??? | ||
override def take(n: Int): String = ??? | ||
override def takeWhile(p: Char => Boolean): String = ??? | ||
override def drop(n: Int): String = ??? | ||
override def dropWhile(p: Char => Boolean): String = ??? | ||
override def slice(from: Int, until: Int): String = ??? | ||
override def map[B](f: Char => B): Iterable[B] = ??? | ||
override def flatMap[B](f: Char => IterableOnce[B]): Iterable[B] = ??? | ||
override def flatten[B](implicit asIterable: Char => IterableOnce[B]): Iterable[B] = ??? | ||
override def collect[B](pf: PartialFunction[Char,B]): Iterable[B] = ??? | ||
override def zipWithIndex: Iterable[(Char, Int)] = ??? | ||
override def span(p: Char => Boolean): (String, String) = ??? | ||
override def tapEach[U](f: Char => U): String = ??? | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.