Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging functional code in Scala

Debugging functional code is definitely more tricky than debugging imperative code. See discussions here, here and here. "Functional" debugging should support inspecting the return value of functions/closures/monads. Do any debuggers/IDEs have (plan to have) the ability to inspect intermediate return values?

For example, to debug this line in Scala, I should be able to step through 4 function invocations and inspect the returned value at each step before returning r

val r=(ls filter (_>1) sort (_<_) zipWithIndex) filter {v=>(v._2)%2==0} map{_._1} 
like image 639
Adrian Avatar asked Nov 25 '10 00:11

Adrian


1 Answers

I think everybody's advice to break this thing down to more manageable chunks is the best approach. One trick for debugging smaller expressions is to steal Ruby's tap function, as described here. "tap" allows you to stick an expression in the middle of a chain like this, and perhaps print out some debug values, like so:

val ls = List(1,2,3).map(_ * 2)                 .tap(soFar => println("So far: " + soFar))                 .map(_ * 2) println(ls) 

This will print out:

So far: List(2, 4, 6)
List(4, 8, 12)

It helps me every once in a while.

like image 162
Adam Rabung Avatar answered Oct 13 '22 00:10

Adam Rabung