Skip to content

Commit

Permalink
Merge pull request twitter#452 from vidma/features/map-aggregator-wit…
Browse files Browse the repository at this point in the history
…h-one-entry

Add MapAggregator from 1 (key, aggregator) pair
  • Loading branch information
johnynek committed Jun 26, 2015
2 parents 708f4a8 + cca1349 commit b1e35ac
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,13 @@ class TupleAggregatorsTest extends WordSpec with Matchers {

val MinLongAgg = Aggregator.min[Int].andThenPresent{ _.toLong }

"Create an aggregator from 1 (key, aggregator) pair" in {
val agg: Aggregator[Int, Long, Map[String, Long]] = MapAggregator(
("key1", SizeAgg))
val expectedMap = Map("key1" -> 6)
assert(agg(data) == expectedMap)
}

"Create an aggregator from 2 (key, aggregator) pairs" in {
val agg: Aggregator[Int, Tuple2[Int, Long], Map[String, Long]] = MapAggregator(
("key1", MinLongAgg),
Expand Down
13 changes: 11 additions & 2 deletions project/GenTupleAggregators.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,17 @@ object MapAggregator {
}

def genMapMethods(max: Int, isMonoid: Boolean = false): String = {
val aggType = if (isMonoid) "Monoid" else ""

// there's no Semigroup[Tuple1[T]], so just use T as intermediary type instead of Tuple1[T]
val aggregatorForOneItem = s"""
|def apply[K, A, B, C](aggDef: (K, ${aggType}Aggregator[A, B, C])): ${aggType}Aggregator[A, B, Map[K, C]] = {
| val (key, agg) = aggDef
| agg.andThenPresent(value => Map(key -> value))
|}
""".stripMargin

(2 to max).map(i => {
val aggType = if (isMonoid) "Monoid" else ""
val nums = (1 to i)
val bs = nums.map("B" + _).mkString(", ")
val aggs = nums.map(x => "agg%s: Tuple2[K, %sAggregator[A, B%s, C]]".format(x, aggType, x)).mkString(", ")
Expand All @@ -90,6 +99,6 @@ def apply[K, A, %s, C](%s): %sAggregator[A, %s, Map[K, C]] = {
prepares,
semiType, semigroup,
tupleBs, present)
}).mkString("\n")
}).mkString("\n") + aggregatorForOneItem
}
}

0 comments on commit b1e35ac

Please # to comment.