From 05184053c557ea5ac62732b0e4cf306f16076543 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Miguel=20Mej=C3=ADa=20Su=C3=A1rez?= Date: Wed, 28 Apr 2021 11:08:49 -0500 Subject: [PATCH] Applying naming suggestions --- .../next/NextIterableOnceOpsExtensions.scala | 51 +++++++++---------- .../next/TestIterableOnceExtensions.scala | 22 ++++---- 2 files changed, 35 insertions(+), 38 deletions(-) diff --git a/src/main/scala/scala/collection/next/NextIterableOnceOpsExtensions.scala b/src/main/scala/scala/collection/next/NextIterableOnceOpsExtensions.scala index 0e3c29f..c675f5c 100644 --- a/src/main/scala/scala/collection/next/NextIterableOnceOpsExtensions.scala +++ b/src/main/scala/scala/collection/next/NextIterableOnceOpsExtensions.scala @@ -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 @@ -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)) { @@ -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())) diff --git a/src/test/scala/scala/collection/next/TestIterableOnceExtensions.scala b/src/test/scala/scala/collection/next/TestIterableOnceExtensions.scala index 0478f8e..d626ec5 100644 --- a/src/test/scala/scala/collection/next/TestIterableOnceExtensions.scala +++ b/src/test/scala/scala/collection/next/TestIterableOnceExtensions.scala @@ -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 @@ -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( @@ -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( @@ -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(