EchoEvaluation

EchoEvaluation[expr]

prints expr before evaluation, then prints the result after evaluation and returns that result.

EchoEvaluation[expr,label]

prepends label when printing expr before and after evaluation.

EchoEvaluation[expr,label1label2]

prepends label1 before evaluation and label2 after evaluation.

EchoEvaluation[expr,labels,f]

prints expr before evaluation, then evaluates expr to the result res and prints f[res].

EchoEvaluation[expr,labels,gf]

prints g[expr] before evaluating expr and then f[res] after evaluation.

Details

Examples

open allclose all

Basic Examples  (3)

Print an intermediate computation before and after evaluation:

x=1+1
2

Print an intermediate evaluation with labels:

"sum:" 1+1
"result:" 2

Report a property of the result of evaluation:

"input:" FactorInteger[250-1]
"factors:" 7

Scope  (5)

Print an expression before and after evaluation:

23
8

Prepend labels before and after evaluation:

"sum:"
"result:" 1

Specify print functions for before and after evaluation:

"input:" RowBox[{"b","+","a"}]
"framed:" a+b

EchoEvaluation can be nested:

Accumulate[EchoEvaluation[Reverse[EchoEvaluation[Range[10]]]]]
Reverse[EchoEvaluation[Range[10]]]
Range[10]
{1,2,3,4,5,6,7,8,9,10}
{10,9,8,7,6,5,4,3,2,1}
{10,19,27,34,40,45,49,52,54,55}

To avoid printing nested EchoEvaluation expressions, use this function:

EchoEvaluation does not appear in the printed expressions:

Accumulate[Reverse[Range[10]]]
Reverse[Range[10]]
Range[10]
{1,2,3,4,5,6,7,8,9,10}
{10,9,8,7,6,5,4,3,2,1}
{10,19,27,34,40,45,49,52,54,55}

Properties & Relations  (5)

EchoEvaluation returns the expression in its first argument:

expr
expr

EchoEvaluation prints the expression before and after evaluation:

1+1
2

Echo only prints the expression after evaluation:

EchoEvaluation uses the same label before and after evaluation:

"words:" StringSplit["This is a sentence"]
"words:" {"This","is","a","sentence"}

Specify different labels for before and after evaluation:

"sentence:" StringSplit["This is a sentence"]
"words:" {"This","is","a","sentence"}

EchoEvaluation[expr,label,f] uses f to print the result after evaluation:

"sum:" 1+1
"sum:" Hold[2]

In EchoEvaluation[expr,label,gf], the function g is given the unevaluated expression and f is given the evaluated expression:

"sum:" Hold[1+1]
"sum:" HoldComplete[2]

EchoEvaluation[expr,label] is effectively equivalent to EchoEvaluation[expr,label,UnevaluatedIdentity]:

label Print["Hello"]
label Null
label Print["Hello"]
label Null

Possible Issues  (1)

In EchoEvaluation[expr,label,gf], the function g must have a holding attribute like HoldFirst or expr will be evaluated twice:

"increment:"
"increment:" 1

Since Panel does not hold its arguments, the symbol x has been incremented twice:

Wolfram Research (2020), EchoEvaluation, Wolfram Language function, https://reference.wolfram.com/language/ref/EchoEvaluation.html.

Text

Wolfram Research (2020), EchoEvaluation, Wolfram Language function, https://reference.wolfram.com/language/ref/EchoEvaluation.html.

CMS

Wolfram Language. 2020. "EchoEvaluation." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/EchoEvaluation.html.

APA

Wolfram Language. (2020). EchoEvaluation. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/EchoEvaluation.html

BibTeX

@misc{reference.wolfram_2024_echoevaluation, author="Wolfram Research", title="{EchoEvaluation}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/EchoEvaluation.html}", note=[Accessed: 17-November-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_echoevaluation, organization={Wolfram Research}, title={EchoEvaluation}, year={2020}, url={https://reference.wolfram.com/language/ref/EchoEvaluation.html}, note=[Accessed: 17-November-2024 ]}