Last active
December 18, 2015 15:58
-
-
Save acruise/5807603 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
| /** | |
| * Splits the in collection into three collections, based on whether each element is assignable to X, Y or Z (in that order). | |
| */ | |
| def filterByType3[X <: AnyRef : Manifest, Y <: AnyRef : Manifest, Z <: AnyRef : Manifest](in: Traversable[_]): (Seq[X], Seq[Y], Seq[Z]) = { | |
| val clax = manifest[X].runtimeClass | |
| val clay = manifest[Y].runtimeClass | |
| val claz = manifest[Z].runtimeClass | |
| def _filterByType3(in: Seq[_], xs: Vector[X], ys: Vector[Y], zs: Vector[Z]): (Seq[X], Seq[Y], Seq[Z]) = { | |
| in match { | |
| case Seq() => (xs, ys, zs) | |
| case Seq(a, as @ _*) => | |
| if (clax.isInstance(a)) _filterByType3(as, xs :+ a.asInstanceOf[X], ys, zs ) | |
| else if (clay.isInstance(a)) _filterByType3(as, xs, ys :+ a.asInstanceOf[Y], zs ) | |
| else if (claz.isInstance(a)) _filterByType3(as, xs, ys , zs :+ a.asInstanceOf[Z]) | |
| else _filterByType3(as, xs, ys, zs) | |
| } | |
| } | |
| _filterByType3(in.toSeq, Vector[X](), Vector[Y](), Vector[Z]()) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment