Print debugging statement: Difference between revisions

J
(Applesoft BASIC)
(J)
Line 354:
value: &main.point{x:2, y:3}
</pre>
 
=={{header|J}}==
J does not provide any specialized debugging statements. So we might typically use <code>echo</code>. But, we could define <code>dump=:{{y[echo(,:x,':&nbsp;'),&.|:1&nbsp;1}._1&nbsp;_1}.":<y }}</code> which would let us include informative labels in the debugging output. Thus, for example, we could use function composition to see what's going on inside a J expression like this:<lang J> $i.3 3
3 3
$'a' dump i.3 3
a: 0 1 2
3 4 5
6 7 8
3 3</lang> Or, like this:<lang J> +/\2 3 5 7
2 5 10 17
' sum'&dump@+/@('list'&dump)\2 3 5 7
list: 2
list: 2 3
sum: 5
list: 2 3 5
sum: 8
sum: 10
list: 2 3 5 7
sum: 12
sum: 15
sum: 17
2 5 10 17</lang>Caution: this approach disables J's internal optimizations for frequently used expressions (in the above example, J uses associativity to increase the performance of +/\ from O(n^2) to O(n), but the <tt>dump</tt> version gains no such benefit).<br /><br />
That said, we could also rely on J's debugging information if we did not want to provide explicit labels for everything (this would not usefully distinguish between different points on the same line). For example: <lang j>dump=: {{
if.#d=.13!:13'' do.
echo (,:': ',~;:inv":&.>0 2{1{d) ,&.|:1 1}._1 _1}.":<y
end.
y
}}</lang>
 
Here, we get debugging output only if debugging is enabled. And we're annotating each value with the name of the routine, and the line within that routine. Thus:
<lang J>example=: {{
j=. 1
dump r=. 2
j+r
}}
 
example''
3
13!:0]1 NB. enable debugging
example''
example 1: 2
3</lang>
 
 
 
 
=={{header|Java}}==
6,951

edits