How to trace function evaluation in Scala?

I study Funcional Programming in Scala, and quite often I need to trace the evaluation of a function to better understand how it works.

For example, having the following function:

def foldRight[A,B](l: List[A], z: B)(f: (A, B) => B): B =
  l match {
    case Nil => z
    case Cons(x, xs) => f(x, foldRight(xs, z)(f))
  }

For the next call:

foldRight(Cons(1, Cons(2, Cons(3, Nil))), 0)(_ + _)

I would like to print my evaluation footprint, for example:

foldRight(Cons(1, Cons(2, Cons(3, Nil))), 0)(_ + _)
1 + foldRight(Cons(2, Cons(3, Nil)), 0)(_ + _)
1 + (2 + foldRight(Cons(3, Nil), 0)(_ + _))
1 + (2 + (3 + (foldRight(Nil, 0)(_ + _))))
1 + (2 + (3 + (0)))
6

Currently I am doing it either manually or by introducing an ugly print. How can I achieve this in a comfortable, elegant manner?

+4
source share
1 answer

I assumed that Cons and :: are the same operations. If you do not mind receiving only the current cell and battery, you can do the following:

def printable(x:Int, y:Int): Int = {
    println("Curr: "+x.toString+" Acc:"+ y.toString)
    x+y
} 
foldRight(List(1, 2, 3, 4), 0)(printable(_,_))  
//> Curr: 4 Acc:0
//| Curr: 3 Acc:4
//| Curr: 2 Acc:7
//| Curr: 1 Acc:9
//| res0: Int = 10

, "-" , , :

def foldRight[A, B](l: List[A], z: B)(f: (A, B) => B): B = {
  var acc = if (l.isEmpty) "" else l.head.toString
  def newAcc(acc: String, x: A) = acc + " + (" + x
  def rightSide(xs: List[A], z: B, size: Int) = xs.toString + "," + z + ")" * (l.size - size + 1)
  def printDebug(left: String, right: String) = println(left + " + foldRight(" + right)

  def go(la: List[A], z: B)(f: (A, B) => B): B = la match {
    case Nil => z
    case x :: xs => {
      acc = newAcc(acc, x)
      printDebug(acc, rightSide(xs, z, la.size))
      f(x, go(xs, z)(f))
    }
  }
  if (l.isEmpty) z
  else f(l.head, go(l.tail, z)(f))
}

: "acc", "go"


, , foldRight.

class Trace[A](z: A) {
  var list = List[A]()
  def store(x: A) = {
    list = list :+ x
  }

  def getTrace(level: Int): String = {
    val left = list.take(level).map(x => s"$x + (").mkString
    val right = list.drop(level).map(x => s"$x,").mkString
    if (right.isEmpty)
      s"${left.dropRight(4)}" + ")" * (list.size - 1)
    else
      s"${left}foldRight(List(${right.init}), $z)" + ")" * (list.size - level - 1)
  }

  def getFullTrace: String =
    { for (i <- 0 to list.size) yield getTrace(i) }.mkString("\n")

  def foldRight(l: List[A], z: A)(f: (A, A) => A): A = l match {
    case Nil => z
    case x :: xs => store(x); f(x, foldRight(xs, z)(f))
  }
}

val start = 0
val t = new Trace[Int](start)
t.foldRight(List(1, 2, 3, 4), start)(_ + _) 
t.getFullTrace 
+1

Source: https://habr.com/ru/post/1526831/


All Articles