Advertisement
Guest User

Untitled

a guest
Sep 10th, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.00 KB | None | 0 0
  1. package object collection {
  2.  
  3.   /**
  4.    * Provides extra util methods for the [[Traversable]] class.
  5.    *
  6.    * @param traversable The [[Traversable]] to enrich.
  7.    * @tparam A The [[Traversable]] type parameter.
  8.    */
  9.   implicit class RichTraversable[+A](traversable: Traversable[A]) {
  10.  
  11.     /**
  12.      * Groups the traversable by the criteria specified by ``groupBy`` and then filters each
  13.      * group by the minimum element according to ``minBy``.
  14.      *
  15.      * @param groupBy The group by function.
  16.      * @param minBy The min by function.
  17.      * @param cmp An implicit ordering for ``B``.
  18.      * @tparam K The type for the groups keys.
  19.      * @tparam B The resulting type of the min by function.
  20.      * @return The traversable with the minimum elements of each group.
  21.      */
  22.     def groupByAndFilterMin[K, B](groupBy: A => K, minBy: A => B)
  23.                                  (implicit cmp: Ordering[B]): Traversable[A] =
  24.       traversable.groupBy(groupBy).mapValues(_.minBy(minBy)).values.toSeq
  25.  
  26.   }
  27.  
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement