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}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With