Is there a clever way to insert a println
for debugging into a partial function such as the one given to foldLeft
below? And if so, how to access the argument?
def pairList[B](as: List[B]): (List[(B, B)], Option[B]) = {
val none: Option[B] = None
val nil: List[(B, B)] = Nil
as.foldLeft((nil, none)) { // would like to println(...) here
case ((stack: List[(B, B)], Some(b2: B)), b1: B) => (((b1, b2) :: stack), none)
case ((stack: List[(B, B)], None), b1: B) => (stack, Some(b1))
} match {
case (stack, leftover) => (stack.reverse, leftover)
}
}
This is what I usually do, but it seems like there should be an easier way.
def pairList[B](as: List[B]): (List[(B, B)], Option[B]) = {
val none: Option[B] = None
val nil: List[(B, B)] = Nil
as.foldLeft((nil, none)) { (acc,next) =>
println(s"acc=$acc next=$next")
(acc,next ) match {
case ((stack: List[(B, B)], Some(b1: B)), b2: B) => (((b1, b2) :: stack), none)
case ((stack: List[(B, B)], None), b1: B) => (stack, Some(b1))
} } match {
case (stack, leftover) => (stack.reverse, leftover)
}
}