How do I apply something like the double slash notation but not at the end of line?
The problem, as usual, is precedence. You need to use parenthesis to group expressions. The code
Scan[ (#^2 // Print) &, {1, 2, 3}]
will now do what you want. Your reference to "mathematica pipe output" is a good idea. You can think of//
as the Mathematica equivalent to the Unix pipe symbol |
or better yet the Forth postfix notation for executing "words" which use the data stack to operate on. In fact, Mathematica itself has an "evaluation stack" accessed using theStack[]
and related functions.
To obtain the "pipeline" style with the data at the beginning of the expression, we can write:
{1, 2, 3} // Scan[f /* Print]
This combines f
and Print
into a single function using /* (right composition) and then uses the operator form of Scan to lift that function so that it operates upon lists.
This composed function is then applied to the list {1, 2, 3}
using the postfix notation //.
We can dispense with brackets altogether by adding some infix notation...
f /* Print ~Scan~ {1, 2, 3}
... but perhaps this is taking it too far (and the initial value is no longer at the start of the pipeline).
Another way to get a similar visual result would be to write:
{1, 2, 3} // Map[f] // Column
If you are interested in combining operators in a concatenative style, you might want to check out Query. For example:
{1, 2, 3} // Query[Column, f]
These are different ways to write the same:
(f[#] // Print) &
Print@f[#] &
Print[f[#]] &
f[#] // Print &
is parsed as (f[#]) // (Print &)
—mind the precendence.
The following are also effectively equivalent (though they denote a different expression):
Print @* f
f /* Print
See Composition
, RightComposition