Skip to content

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 5 commits into from
Apr 5, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/main/scala/scala/collection/next/IterableOnceExtensions.scala
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

object IterableOnceExtensions {
implicit class NextIterableOnceOpsExtensions[A](private val iter: IterableOnceOps[A, Any, Any]) 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]
iter.foreach { elem =>
m.updateWith(key = key(elem)) {
case Some(b) => Some(reduce(b, f(elem)))
case None => Some(f(elem))
}
}
m.to(immutable.Map)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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

import IterableOnceExtensions._

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](coll: IterableOnceOps[A, Any, Any]): 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 = ???
}
}