How do I apply something like the double slash notation but not at the end of line?












5












$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".










share|improve this question









$endgroup$

















    5












    $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".










    share|improve this question









    $endgroup$















      5












      5








      5





      $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".










      share|improve this question









      $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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 6 hours ago









      seilguseilgu

      783




      783






















          4 Answers
          4






          active

          oldest

          votes


















          5












          $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






          share|improve this answer









          $endgroup$













          • $begingroup$
            Use ; to suppress output of Nulls: Print@*f /@ Range@3;
            $endgroup$
            – Bob Hanlon
            2 hours ago



















          5












          $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.






          share|improve this answer











          $endgroup$





















            2












            $begingroup$

            Another option by using the true name of &:



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





            share|improve this answer









            $endgroup$





















              2












              $begingroup$

              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






              share|improve this answer











              $endgroup$













                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
                });


                }
                });














                draft saved

                draft discarded


















                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









                5












                $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






                share|improve this answer









                $endgroup$













                • $begingroup$
                  Use ; to suppress output of Nulls: Print@*f /@ Range@3;
                  $endgroup$
                  – Bob Hanlon
                  2 hours ago
















                5












                $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






                share|improve this answer









                $endgroup$













                • $begingroup$
                  Use ; to suppress output of Nulls: Print@*f /@ Range@3;
                  $endgroup$
                  – Bob Hanlon
                  2 hours ago














                5












                5








                5





                $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






                share|improve this answer









                $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







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 6 hours ago









                SzabolcsSzabolcs

                160k13436933




                160k13436933












                • $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
















                $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











                5












                $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.






                share|improve this answer











                $endgroup$


















                  5












                  $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.






                  share|improve this answer











                  $endgroup$
















                    5












                    5








                    5





                    $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.






                    share|improve this answer











                    $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.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 28 mins ago

























                    answered 6 hours ago









                    SomosSomos

                    97419




                    97419























                        2












                        $begingroup$

                        Another option by using the true name of &:



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





                        share|improve this answer









                        $endgroup$


















                          2












                          $begingroup$

                          Another option by using the true name of &:



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





                          share|improve this answer









                          $endgroup$
















                            2












                            2








                            2





                            $begingroup$

                            Another option by using the true name of &:



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





                            share|improve this answer









                            $endgroup$



                            Another option by using the true name of &:



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






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 1 hour ago









                            b3m2a1b3m2a1

                            27.7k357161




                            27.7k357161























                                2












                                $begingroup$

                                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






                                share|improve this answer











                                $endgroup$


















                                  2












                                  $begingroup$

                                  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






                                  share|improve this answer











                                  $endgroup$
















                                    2












                                    2








                                    2





                                    $begingroup$

                                    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






                                    share|improve this answer











                                    $endgroup$



                                    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







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited 53 mins ago

























                                    answered 1 hour ago









                                    WReachWReach

                                    53.1k2114211




                                    53.1k2114211






























                                        draft saved

                                        draft discarded




















































                                        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.




                                        draft saved


                                        draft discarded














                                        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





















































                                        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







                                        Popular posts from this blog

                                        Aikido

                                        Tivadar Csontváry Kosztka

                                        Metroo de Marsejlo