Skip to content

Commit

Permalink
Applying naming suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
BalmungSan committed Apr 28, 2021
1 parent 301d839 commit 0518405
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ package next
private[next] final class NextIterableOnceOpsExtensions[A, CC[_], C](
private val col: IterableOnceOps[A, CC, C]
) extends AnyVal {
import NextIterableOnceOpsExtensions.{GroupMapGen, GroupMapGenGen}
import NextIterableOnceOpsExtensions.{GroupMapToView, GroupMapView}

def groupBy[K](key: A => K)(implicit valuesFactory: Factory[A, C]): immutable.Map[K, C] =
groupByGen(key).result
def groupBy[K](key: A => K)(implicit groupsFactory: Factory[A, C]): immutable.Map[K, C] =
viewGroupByTo(key).toMap

def groupByGen[K](key: A => K)(implicit valuesFactory: Factory[A, C]): GroupMapGen[A, K, A, C] =
groupByGenGen(key).collectValuesAs(valuesFactory)
def viewGroupByTo[K](key: A => K)(implicit groupsFactory: Factory[A, C]): GroupMapToView[A, K, A, C] =
viewGroupBy(key).collectGroupsTo(groupsFactory)

def groupByGenGen[K](key: A => K): GroupMapGenGen[A, K, A] =
groupMapGenGen(key)(identity)
def viewGroupBy[K](key: A => K): GroupMapView[A, K, A] =
viewGroupMap(key)(identity)

def groupMap[K, V](key: A => K)(f: A => V)(implicit valuesFactory: Factory[V, CC[V]]): immutable.Map[K, CC[V]] =
groupMapGen(key)(f).result
def groupMap[K, V](key: A => K)(f: A => V)(implicit groupsFactory: Factory[V, CC[V]]): immutable.Map[K, CC[V]] =
viewGroupMapTo(key)(f).toMap

def groupMapGen[K, V](key: A => K)(f: A => V)(implicit valuesFactory: Factory[V, CC[V]]): GroupMapGen[A, K, V, CC[V]] =
groupMapGenGen(key)(f).collectValuesAs(valuesFactory)
def viewGroupMapTo[K, V](key: A => K)(f: A => V)(implicit groupsFactory: Factory[V, CC[V]]): GroupMapToView[A, K, V, CC[V]] =
viewGroupMap(key)(f).collectGroupsTo(groupsFactory)

def groupMapGenGen[K, V](key: A => K)(f: A => V): GroupMapGenGen[A, K, V] =
new GroupMapGenGen(col, key, f)
def viewGroupMap[K, V](key: A => K)(f: A => V): GroupMapView[A, K, V] =
new GroupMapView(col, key, f)

/**
* Partitions this IterableOnce into a map according to a discriminator function `key`. All the values that
Expand All @@ -49,19 +49,16 @@ private[next] final class NextIterableOnceOpsExtensions[A, CC[_], C](
* @note This will force the evaluation of the Iterator.
*/
def groupMapReduce[K, V](key: A => K)(f: A => V)(reduce: (V, V) => V): immutable.Map[K, V] =
groupMapGenGen(key)(f).reduceValues(reduce)
viewGroupMap(key)(f).reduceValuesTo(immutable.Map)(reduce)
}

private[next] object NextIterableOnceOpsExtensions {
final class GroupMapGenGen[A, K, V] private[NextIterableOnceOpsExtensions](
final class GroupMapView[A, K, V] private[NextIterableOnceOpsExtensions](
col: IterableOnceOps[A, AnyConstr, _],
key: A => K,
f: A => V
) {
def reduceValues(reduce: (V, V) => V): immutable.Map[K, V] =
reduceValuesAs(immutable.Map)(reduce)

def reduceValuesAs[MC](resultFactory: Factory[(K, V), MC])(reduce: (V, V) => V): MC = {
def reduceValuesTo[MC](resultFactory: Factory[(K, V), MC])(reduce: (V, V) => V): MC = {
val m = mutable.Map.empty[K, V]
col.foreach { elem =>
m.updateWith(key = key(elem)) {
Expand All @@ -72,27 +69,27 @@ private[next] object NextIterableOnceOpsExtensions {
resultFactory.fromSpecific(m)
}

def collectValuesAs[C](valuesFactory: Factory[V, C]): GroupMapGen[A, K, V, C] =
new GroupMapGen(col, key, f, valuesFactory)
def collectGroupsTo[C](groupsFactory: Factory[V, C]): GroupMapToView[A, K, V, C] =
new GroupMapToView(col, key, f, groupsFactory)
}

final class GroupMapGen[A, K, V, C] private[NextIterableOnceOpsExtensions](
final class GroupMapToView[A, K, V, C] private[NextIterableOnceOpsExtensions](
col: IterableOnceOps[A, AnyConstr, _],
key: A => K,
f: A => V,
valuesFactory: Factory[V, C]
groupsFactory: Factory[V, C]
) {
def result: immutable.Map[K, C] =
resultAs(immutable.Map)
def toMap: immutable.Map[K, C] =
to(immutable.Map)

def resultAs[MC](resultFactory: Factory[(K, C), MC]): MC = {
def to[MC](resultFactory: Factory[(K, C), MC]): MC = {
val m = mutable.Map.empty[K, mutable.Builder[V, C]]
col.foreach { elem =>
val k = key(elem)
val v = f(elem)
m.get(k) match {
case Some(builder) => builder.addOne(v)
case None => m.update(key = k, value = valuesFactory.newBuilder.addOne(v))
case None => m.update(key = k, value = groupsFactory.newBuilder.addOne(v))
}
}
resultFactory.fromSpecific(m.view.mapValues(_.result()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ final class TestIterableOnceExtensions {

// GroupMapGenGen --------------------------------------------
@Test
def anyCollectionGroupMapGenResultAs(): Unit = {
def anyCollectionGroupMapToViewTo(): Unit = {
def getUniqueUsersByCountrySorted(data: List[Record]): List[(String, List[String])] =
data
.groupMapGenGen(_.country)(_.user)
.collectValuesAs(SortedSet)
.resultAs(SortedMap)
.viewGroupMap(_.country)(_.user)
.collectGroupsTo(SortedSet)
.to(SortedMap)
.view
.mapValues(_.toList)
.toList
Expand All @@ -98,11 +98,11 @@ final class TestIterableOnceExtensions {
}

@Test
def anyCollectionGroupMapGenGenReduce(): Unit = {
def anyCollectionGroupMapViewReduceValuesTo(): Unit = {
def getAllWordsByFirstLetterSorted(data: List[String]): List[(Char, String)] =
data
.groupByGenGen(_.head)
.reduceValuesAs(SortedMap)(_ ++ " " ++ _)
.viewGroupBy(_.head)
.reduceValuesTo(SortedMap)(_ ++ " " ++ _)
.toList

val data = List(
Expand All @@ -125,9 +125,9 @@ final class TestIterableOnceExtensions {
}

@Test
def iterableOnceOpsGroupByGenSpecificFactory(): Unit = {
def iterableOnceOpsViewGroupByToSpecificFactoryToMap(): Unit = {
def bitsByEven(data: BitSet): Map[Boolean, BitSet] =
data.groupByGen(x => (x % 2) == 0).result
data.viewGroupByTo(x => (x % 2) == 0).toMap

val data = BitSet(1, 2, 3, 4, 5)
val expected = Map(
Expand All @@ -139,9 +139,9 @@ final class TestIterableOnceExtensions {
}

@Test
def iterableOnceOpsGroupMapGenIterableFactory(): Unit = {
def iterableOnceOpsViewGroupMapToIterableFactoryToMap(): Unit = {
def bitsByEvenAsChars(data: BitSet): Map[Boolean, Set[Char]] =
data.groupMapGen(x => (x % 2) == 0)(_.toChar).result
data.viewGroupMapTo(x => (x % 2) == 0)(_.toChar).toMap

val data = BitSet(100, 101, 102, 103, 104, 105)
val expected = Map(
Expand Down

0 comments on commit 0518405

Please sign in to comment.