def toPairs[A](xs : Seq[A]) : Seq[Tuple2[A,A]] = {
@tailrec
def accumulator(as : Seq[A], accum: Seq[Tuple2[A,A]]) : Seq[Tuple2[A,A]] = as match {
case Nil => accum
case x :: tail => tail match {
case Nil => accum
case _ => accumulator(as.tail, accum :+ (x,tail.head))
}
}
accumulator(xs, Seq.empty[Tuple2[A,A]])
}
I use this code for the following use case: for a Seq[A] i.e. List(1,2,3) I want a Seq of Tuples of A i.e. List((1,2)(2,3)). Is this the most optimal, idiomatic way in Scala, what could be improved?
For the record: the order of the elements is important and is expected to be congruent with the example in the preceding paragraph.