f[x_] = x^2;
Scan[Print[f[#]] &, {1, 2, 3}]

I want to do something like this, but I don't want to type so many brackets. I'd like to have something like

f[x_] = x^2;
Scan[f[#] //Print &, {1, 2, 3}]

But this doesn't work. What's the correct symbol/method, to pipe the output of f[#] to Print ?

Sorry I googled "mathematica pipe output" but I don't think I'm using the correct keywords. But linux users have the terminology of "piping".


4 Answers 4


To obtain the "pipeline" style with the data at the beginning of the expression, we can write:

{1, 2, 3} // Scan[f /* Print]

printed output

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

columnar output

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]

query output


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.


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

  • 1
    $\begingroup$ Use ; to suppress output of Nulls: Print@*f /@ Range@3; $\endgroup$
    – Bob Hanlon
    Commented Feb 9, 2019 at 22:21

Another option by using the true name of &:

f[x_] = x^2;
Scan[f[#] //Print //Function, {1, 2, 3}]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.