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












10












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

















    10












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















      10












      10








      10


      1



      $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 Feb 9 at 18:26









      seilguseilgu

      1726




      1726






















          4 Answers
          4






          active

          oldest

          votes


















          8












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





















            9












            $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" accessed using theStackand related functions.






            share|improve this answer











            $endgroup$





















              8












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









              • 1




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



















              8












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














                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









                8












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


















                  8












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
















                    8












                    8








                    8





                    $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 Feb 9 at 23:51

























                    answered Feb 9 at 23:21









                    WReachWReach

                    53.5k2115214




                    53.5k2115214























                        9












                        $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" accessed using theStackand related functions.






                        share|improve this answer











                        $endgroup$


















                          9












                          $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" accessed using theStackand related functions.






                          share|improve this answer











                          $endgroup$
















                            9












                            9








                            9





                            $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" accessed using theStackand related functions.






                            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" accessed using theStackand related functions.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Feb 10 at 1:05

























                            answered Feb 9 at 18:33









                            SomosSomos

                            1,7451110




                            1,7451110























                                8












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









                                • 1




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
















                                8












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









                                • 1




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














                                8












                                8








                                8





                                $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 Feb 9 at 18:35









                                SzabolcsSzabolcs

                                163k14447944




                                163k14447944








                                • 1




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














                                • 1




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








                                1




                                1




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




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











                                8












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


















                                  8












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
















                                    8












                                    8








                                    8





                                    $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 Feb 9 at 23:30









                                    b3m2a1b3m2a1

                                    28.5k359164




                                    28.5k359164






























                                        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

                                        Human spaceflight

                                        Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?

                                        張江高科駅