File tree 2 files changed +16
-0
lines changed
algebird-core/src/main/scala/com/twitter/algebird
2 files changed +16
-0
lines changed Original file line number Diff line number Diff line change @@ -102,6 +102,16 @@ object Aggregator extends java.io.Serializable {
102
102
*/
103
103
def sortedReverseTake [T : Ordering ](count : Int ): MonoidAggregator [T , PriorityQueue [T ], Seq [T ]] =
104
104
new mutable.PriorityQueueToListAggregator [T ](count)(implicitly[Ordering [T ]].reverse)
105
+ /**
106
+ * Immutable version of sortedTake, for frameworks that check immutability of reduce functions.
107
+ */
108
+ def immutableSortedTake [T : Ordering ](count : Int ): MonoidAggregator [T , TopK [T ], Seq [T ]] =
109
+ new TopKToListAggregator [T ](count)
110
+ /**
111
+ * Immutable version of sortedReverseTake, for frameworks that check immutability of reduce functions.
112
+ */
113
+ def immutableSortedReverseTake [T : Ordering ](count : Int ): MonoidAggregator [T , TopK [T ], Seq [T ]] =
114
+ new TopKToListAggregator [T ](count)(implicitly[Ordering [T ]].reverse)
105
115
/**
106
116
* Put everything in a List. Note, this could fill the memory if the List is very large.
107
117
*/
Original file line number Diff line number Diff line change @@ -87,3 +87,9 @@ class TopKMonoid[T](k: Int)(implicit ord: Ordering[T]) extends Monoid[TopK[T]] {
87
87
TopK (size, reversed, max)
88
88
}
89
89
}
90
+
91
+ class TopKToListAggregator [A ](max : Int )(implicit ord : Ordering [A ]) extends MonoidAggregator [A , TopK [A ], List [A ]] {
92
+ val monoid : Monoid [TopK [A ]] = new TopKMonoid [A ](max)(ord)
93
+ override def present (a : TopK [A ]): List [A ] = a.items
94
+ override def prepare (a : A ): TopK [A ] = TopK (1 , List (a), Some (a))
95
+ }
You can’t perform that action at this time.
0 commit comments