How do I apply something like the double slash notation but not at the end of line?
$begingroup$
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".
functions
$endgroup$
add a comment |
$begingroup$
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".
functions
$endgroup$
add a comment |
$begingroup$
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".
functions
$endgroup$
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".
functions
functions
asked 6 hours ago
seilguseilgu
783
783
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
$begingroup$
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
$endgroup$
$begingroup$
Use;to suppress output ofNulls:Print@*f /@ Range@3;
$endgroup$
– Bob Hanlon
2 hours ago
add a comment |
$begingroup$
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" which can be accessed using theStackfunction.
$endgroup$
add a comment |
$begingroup$
Another option by using the true name of &:
f[x_] = x^2;
Scan[f[#] //Print //Function, {1, 2, 3}]
$endgroup$
add a comment |
$begingroup$
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]

$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "387"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f191196%2fhow-do-i-apply-something-like-the-double-slash-notation-but-not-at-the-end-of-li%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
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
$endgroup$
$begingroup$
Use;to suppress output ofNulls:Print@*f /@ Range@3;
$endgroup$
– Bob Hanlon
2 hours ago
add a comment |
$begingroup$
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
$endgroup$
$begingroup$
Use;to suppress output ofNulls:Print@*f /@ Range@3;
$endgroup$
– Bob Hanlon
2 hours ago
add a comment |
$begingroup$
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
$endgroup$
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
answered 6 hours ago
SzabolcsSzabolcs
160k13436933
160k13436933
$begingroup$
Use;to suppress output ofNulls:Print@*f /@ Range@3;
$endgroup$
– Bob Hanlon
2 hours ago
add a comment |
$begingroup$
Use;to suppress output ofNulls:Print@*f /@ Range@3;
$endgroup$
– Bob Hanlon
2 hours ago
$begingroup$
Use
; to suppress output of Nulls: Print@*f /@ Range@3;$endgroup$
– Bob Hanlon
2 hours ago
$begingroup$
Use
; to suppress output of Nulls: Print@*f /@ Range@3;$endgroup$
– Bob Hanlon
2 hours ago
add a comment |
$begingroup$
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" which can be accessed using theStackfunction.
$endgroup$
add a comment |
$begingroup$
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" which can be accessed using theStackfunction.
$endgroup$
add a comment |
$begingroup$
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" which can be accessed using theStackfunction.
$endgroup$
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" which can be accessed using theStackfunction.
edited 28 mins ago
answered 6 hours ago
SomosSomos
97419
97419
add a comment |
add a comment |
$begingroup$
Another option by using the true name of &:
f[x_] = x^2;
Scan[f[#] //Print //Function, {1, 2, 3}]
$endgroup$
add a comment |
$begingroup$
Another option by using the true name of &:
f[x_] = x^2;
Scan[f[#] //Print //Function, {1, 2, 3}]
$endgroup$
add a comment |
$begingroup$
Another option by using the true name of &:
f[x_] = x^2;
Scan[f[#] //Print //Function, {1, 2, 3}]
$endgroup$
Another option by using the true name of &:
f[x_] = x^2;
Scan[f[#] //Print //Function, {1, 2, 3}]
answered 1 hour ago
b3m2a1b3m2a1
27.7k357161
27.7k357161
add a comment |
add a comment |
$begingroup$
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]

$endgroup$
add a comment |
$begingroup$
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]

$endgroup$
add a comment |
$begingroup$
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]

$endgroup$
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]

edited 53 mins ago
answered 1 hour ago
WReachWReach
53.1k2114211
53.1k2114211
add a comment |
add a comment |
Thanks for contributing an answer to Mathematica Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f191196%2fhow-do-i-apply-something-like-the-double-slash-notation-but-not-at-the-end-of-li%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown