Skip to content

Instantly share code, notes, and snippets.

@kencoba
Created July 14, 2014 08:29
Show Gist options
  • Save kencoba/343e440767c55fc6d222 to your computer and use it in GitHub Desktop.
Save kencoba/343e440767c55fc6d222 to your computer and use it in GitHub Desktop.
worksheet of quotient.
object quotient {
def mkCoset[E](objs: Seq[E], equivalence: (E) => E):Iterable[Seq[E]] = {
objs.groupBy(e => equivalence(e)).map(p => p._2)
} //> mkCoset: [E](objs: Seq[E], equivalence: E => E)Iterable[Seq[E]]
val coset = mkCoset((1 to 5), (e:Int) => e % 3)
//> coset : Iterable[Seq[Int]] = List(Vector(2, 5), Vector(1, 4), Vector(3))
val originalSet = coset.flatten //> originalSet : Iterable[Int] = List(2, 5, 1, 4, 3)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment