I'm trying to make something like this work:
Evaluating
someData // Query[whatever...] /* summarizeInPrettyWay /* promptToRemoveCompoundExpression ;
should produce
(Please remove the semicolon and re-evaluate in order to see a summary of the data)
In words, I want to operate on some data (usually, in order to reduce it into a more manageable data set appropriate for some specific type of operation), prepare a summary of this reduced data set and allow the user of the Notebook
to either see the summary or not (hence why I use a semicolon (;) at the end of the expression).
If the user decides not to remove the semicolon, when evaluating the cell (they evaluate a CompoundExpression
) they will see a message informing them, that there is a summary of the data they just calculated, which they can review by removing the semicolon and re-evaluating the cell.
On the other hand, if the user decides to remove the semicolon (they evaluate a RightComposition
) and evaluate, then the output is some summary description of the data without any message (prompting them to remove a semicolon, in order to see the summary).
The data are in the form of { <|key -> value, ...|>, ... }
.
So far, I am failing.
Code:
This is the summary code:
funcs = {First, Last};
pretty = (Thread[{Length, Element} -> (MapAt[(Thread[funcs -> #]&) /*
Association, #, -1])] &) /* Association;
summary = (Through[{Length, Through[funcs[#]]&}[#]]&) /* pretty;
And here's a toy dataset:
toy = {
<|"date"->"5/1/2021","x"->1,"y"->2,"z"->"alpha","w"->3.14|>,
<|"date"->"6/2/2022","x"->3,"y"->4,"z"->"gamma","w"->2.72|>,
<|"date"->"12/2/2021","x"->5,"y"->6,"z"->"beta","w"->1.41|>,
<|"date"->"3/6/2021","x"->7,"y"->8,"z"->"alpha","w"->1.73|>,
<|"date"->"4/4/2022","x"->9,"y"->10,"z"->"gamma","w"->2.23|>,
<|"date"->"12/1/2021","x"->11,"y"->12,"z"->"alpha","w"->0.57|>,
<|"date"->"5/1/2022","x"->13,"y"->14,"z"->"alpha","w"->1.61|>
};
This is what I came up with:
msg = "Please remove the semicolon and re-evaluate to see a summary";
prompt /: CompoundExpression[__, prompt] := Print[msg]
prompt /: RightComposition[__, prompt] := Identity
And here are the ways it fails:
toy // Query[Select[#z == "alpha" &]] /* summary /* Dataset /* prompt;
doesn't print a message, as it should have.toy // Query[Select[#z == "alpha" &]] /* summary /* Dataset /* prompt
evaluates to
{
<|date->5/1/2021,x->1,y->2,z->alpha,w->3.14|>,
<|date->6/2/2022,x->3,y->4,z->gamma,w->2.72|>,
<|date->12/2/2021,x->5,y->6,z->beta,w->1.41|>,
<|date->3/6/2021,x->7,y->8,z->alpha,w->1.73|>,
<|date->4/4/2022,x->9,y->10,z->gamma,w->2.23|>,
<|date->12/1/2021,x->11,y->12,z->alpha,w->0.57|>,
<|date->5/1/2022,x->13,y->14,z->alpha,w->1.61|>
}
when it should evaluate to
which is the expected output of toy // Query[Select[#z == "alpha" &]] /* summary /* Dataset
(excluding the /* prompt
)