Last active
December 15, 2015 17:18
-
-
Save fthomas/5294915 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.language.higherKinds | |
import scala.collection.TraversableLike | |
def minGroupBy[CC[A], A, B](c: CC[A])(f: A => B) | |
(implicit ev1: CC[A] => TraversableLike[A, CC[A]], | |
ev2: Ordering[B]): CC[A] = { | |
val grouped = c.groupBy(f) | |
if (grouped.nonEmpty) grouped.minBy(_._1)._2 else c | |
} //> minGroupBy: [CC[A], A, B](c: CC[A])(f: A => B)(implicit ev1: CC[A] => scala. | |
//| collection.TraversableLike[A,CC[A]], implicit ev2: Ordering[B])CC[A] | |
minGroupBy(Set("a", "b", "ab"))(_.length) //> res0: scala.collection.immutable.Set[String] = Set(a, b) | |
minGroupBy(List("a", "b", "ab"))(_.length) //> res1: List[String] = List(a, b) | |
minGroupBy(List.empty[String])(_.length) //> res2: List[String] = List() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment