2011-01-19 00:24:11 +01:00
|
|
|
/* sbt -- Simple Build Tool
|
|
|
|
|
* Copyright 2011 Mark Harrah
|
|
|
|
|
*/
|
|
|
|
|
package sbt
|
|
|
|
|
|
2011-05-30 01:17:31 +02:00
|
|
|
object Util
|
2011-01-19 00:24:11 +01:00
|
|
|
{
|
2011-10-10 03:48:15 +02:00
|
|
|
def separateE[A,B](ps: Seq[Either[A,B]]): (Seq[A], Seq[B]) =
|
|
|
|
|
separate(ps)(Types.idFun)
|
|
|
|
|
|
2011-01-19 00:24:11 +01:00
|
|
|
def separate[T,A,B](ps: Seq[T])(f: T => Either[A,B]): (Seq[A], Seq[B]) =
|
2011-09-26 14:20:07 +02:00
|
|
|
{
|
|
|
|
|
val (a,b) = ((Nil: Seq[A], Nil: Seq[B]) /: ps)( (xs, y) => prependEither(xs, f(y)) )
|
|
|
|
|
(a.reverse, b.reverse)
|
|
|
|
|
}
|
2011-01-19 00:24:11 +01:00
|
|
|
|
|
|
|
|
def prependEither[A,B](acc: (Seq[A], Seq[B]), next: Either[A,B]): (Seq[A], Seq[B]) =
|
|
|
|
|
next match
|
|
|
|
|
{
|
|
|
|
|
case Left(l) => (l +: acc._1, acc._2)
|
|
|
|
|
case Right(r) => (acc._1, r +: acc._2)
|
|
|
|
|
}
|
2012-03-12 00:14:27 +01:00
|
|
|
|
|
|
|
|
def pairID[A,B] = (a: A, b: B) => (a,b)
|
|
|
|
|
}
|