changing number of arguments to a function in secondary evaluationusing a Mathematica function to define a new functionConstruct a function whose definition depends on the values of its arguments and evaluates a SectionHow to get the parameter number of inbuilt functionHow to hold evaluation of a value that is passed to a functionFormatting Display PrecisionUsing DeleteCases with a defined function with two arguments as a patternFunctions that remember some arguments while not remembering other arguments

How to respectfully refuse to assist co-workers with IT issues?

Non-visual Computers - thoughts?

Which note goes on which side of the stem?

How do I request a longer than normal leave of absence period for my wedding?

Defense against attacks using dictionaries

Irish Snap: Variant Rules

Would this system work to purify water?

Using `With[...]` with a list specification as a variable

Attaching a piece of wood to a necklace without drilling

Is “I am getting married with my sister” ambiguous?

Are modern clipless shoes and pedals that much better than toe clips and straps?

Did a flight controller ever answer Flight with a no-go?

Did it used to be possible to target a zone?

In the MCU, why does Mjölnir retain its enchantments after Ragnarok?

Why can't an Airbus A330 dump fuel in an emergency?

Why did MS-DOS applications built using Turbo Pascal fail to start with a division by zero error on faster systems?

Does a face-down creature with morph retain its damage when it is turned face up?

Efficiently pathfinding many flocking enemies around obstacles

Why different interest rates for checking and savings?

Can realistic planetary invasion have any meaningful strategy?

Why does wire gauge go down as the physical wire size goes up?

Are illustrations in novels frowned upon?

If the first law of thermodynamics ensures conservation of energy, why does it allow systems to lose energy?

Why does The Ancient One think differently about Doctor Strange in Endgame than the film Doctor Strange?



changing number of arguments to a function in secondary evaluation


using a Mathematica function to define a new functionConstruct a function whose definition depends on the values of its arguments and evaluates a SectionHow to get the parameter number of inbuilt functionHow to hold evaluation of a value that is passed to a functionFormatting Display PrecisionUsing DeleteCases with a defined function with two arguments as a patternFunctions that remember some arguments while not remembering other arguments






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








7












$begingroup$


So I have a function that produces answers involving uninstantiated(?) function names, e.g.



 Out[1]= f[a,b,c,d].


I then want to evalute this leftover result by, say, replacing this f[a,b,c,d] with a defined function g[a,b,c,d,e].



I think something maybe to do with slots, but my poor attempts have still failed, eg.



 Out[1] /. f[##] &@g[##,e]

Out[1] /. f[___] -> g

Out[1] /. f[#1, #2, #3, #4] -> g[#1, #2, #3, #4, e]









share|improve this question









$endgroup$









  • 2




    $begingroup$
    You're mixing the concepts of pure functions (# and &) with replacement (/., ->, :>). Use slots with the first one, use patterns (Pattern in the documentation, x_ or x__ for example in usage) with the second. E.g. f[a, b, c, d] /. f[x__] :> g[x, e].
    $endgroup$
    – eyorble
    Aug 10 at 18:02











  • $begingroup$
    Aargh. This comment is an answer! I'll accept it if you want
    $endgroup$
    – nate
    Aug 10 at 18:06

















7












$begingroup$


So I have a function that produces answers involving uninstantiated(?) function names, e.g.



 Out[1]= f[a,b,c,d].


I then want to evalute this leftover result by, say, replacing this f[a,b,c,d] with a defined function g[a,b,c,d,e].



I think something maybe to do with slots, but my poor attempts have still failed, eg.



 Out[1] /. f[##] &@g[##,e]

Out[1] /. f[___] -> g

Out[1] /. f[#1, #2, #3, #4] -> g[#1, #2, #3, #4, e]









share|improve this question









$endgroup$









  • 2




    $begingroup$
    You're mixing the concepts of pure functions (# and &) with replacement (/., ->, :>). Use slots with the first one, use patterns (Pattern in the documentation, x_ or x__ for example in usage) with the second. E.g. f[a, b, c, d] /. f[x__] :> g[x, e].
    $endgroup$
    – eyorble
    Aug 10 at 18:02











  • $begingroup$
    Aargh. This comment is an answer! I'll accept it if you want
    $endgroup$
    – nate
    Aug 10 at 18:06













7












7








7


1



$begingroup$


So I have a function that produces answers involving uninstantiated(?) function names, e.g.



 Out[1]= f[a,b,c,d].


I then want to evalute this leftover result by, say, replacing this f[a,b,c,d] with a defined function g[a,b,c,d,e].



I think something maybe to do with slots, but my poor attempts have still failed, eg.



 Out[1] /. f[##] &@g[##,e]

Out[1] /. f[___] -> g

Out[1] /. f[#1, #2, #3, #4] -> g[#1, #2, #3, #4, e]









share|improve this question









$endgroup$




So I have a function that produces answers involving uninstantiated(?) function names, e.g.



 Out[1]= f[a,b,c,d].


I then want to evalute this leftover result by, say, replacing this f[a,b,c,d] with a defined function g[a,b,c,d,e].



I think something maybe to do with slots, but my poor attempts have still failed, eg.



 Out[1] /. f[##] &@g[##,e]

Out[1] /. f[___] -> g

Out[1] /. f[#1, #2, #3, #4] -> g[#1, #2, #3, #4, e]






functions argument-patterns






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 10 at 17:59









natenate

2851 silver badge7 bronze badges




2851 silver badge7 bronze badges










  • 2




    $begingroup$
    You're mixing the concepts of pure functions (# and &) with replacement (/., ->, :>). Use slots with the first one, use patterns (Pattern in the documentation, x_ or x__ for example in usage) with the second. E.g. f[a, b, c, d] /. f[x__] :> g[x, e].
    $endgroup$
    – eyorble
    Aug 10 at 18:02











  • $begingroup$
    Aargh. This comment is an answer! I'll accept it if you want
    $endgroup$
    – nate
    Aug 10 at 18:06












  • 2




    $begingroup$
    You're mixing the concepts of pure functions (# and &) with replacement (/., ->, :>). Use slots with the first one, use patterns (Pattern in the documentation, x_ or x__ for example in usage) with the second. E.g. f[a, b, c, d] /. f[x__] :> g[x, e].
    $endgroup$
    – eyorble
    Aug 10 at 18:02











  • $begingroup$
    Aargh. This comment is an answer! I'll accept it if you want
    $endgroup$
    – nate
    Aug 10 at 18:06







2




2




$begingroup$
You're mixing the concepts of pure functions (# and &) with replacement (/., ->, :>). Use slots with the first one, use patterns (Pattern in the documentation, x_ or x__ for example in usage) with the second. E.g. f[a, b, c, d] /. f[x__] :> g[x, e].
$endgroup$
– eyorble
Aug 10 at 18:02





$begingroup$
You're mixing the concepts of pure functions (# and &) with replacement (/., ->, :>). Use slots with the first one, use patterns (Pattern in the documentation, x_ or x__ for example in usage) with the second. E.g. f[a, b, c, d] /. f[x__] :> g[x, e].
$endgroup$
– eyorble
Aug 10 at 18:02













$begingroup$
Aargh. This comment is an answer! I'll accept it if you want
$endgroup$
– nate
Aug 10 at 18:06




$begingroup$
Aargh. This comment is an answer! I'll accept it if you want
$endgroup$
– nate
Aug 10 at 18:06










3 Answers
3






active

oldest

votes


















9













$begingroup$

From my own comment:



You're mixing the concepts of pure functions (# and &) with replacement (/., ->, :>). Use slots with the first one, use patterns (Pattern in the documentation, x_ or x__ for example in usage) with the second.



For this problem:



f[a, b, c, d] /. f[x__] :> g[x, e]



g[a, b, c, d, e]




Note the use of __, which is two underscores: This is a pattern which grabs 1 or more elements (see also ___ which grabs 0 or more elements), and inserts them as a Sequence when used in replacement. Thus, it doesn't get inserted as a List.



If you wanted to do this with pure functions, it becomes a bit more complicated:



g[Sequence @@ #, e] &[f[a, b, c, d]]



g[a, b, c, d, e]




But note that this isn't dependent on its argument being in the form of f[...], it will replace any functional head. To avoid that requires conditionals of some variety, e.g:



If[Head[#] === f, g[Sequence @@ #, e], Undefined] &[f[a, b, c, d]]





share|improve this answer











$endgroup$






















    7













    $begingroup$

    You can also use Apply:



    g[##, e] & @@ f[a, b, c, d]



    g[a, b, c, d, e]




    or ReplaceAll with replacement rule f -> (g[##, e] &):



    f[a, b, c, d] /. f -> (g[##, e] &)



    g[a, b, c, d, e]







    share|improve this answer









    $endgroup$






















      2













      $begingroup$

      There is yet another solution, using Flatten with Head f, namely



      Flatten[g[f[a, b, c, d], e], 1, f]



      g[a, b, c, d, e]




      This assumes that g is not defined for two arguments, otherwise it will evaluate before Flatten has a chance so flatten the f. If that's not the case one can use Inactive on g, act with Flatten and then Activate again.



      Activate[Flatten[Inactive[g][f[a, b, c, d], e], 1, f]]





      share|improve this answer









      $endgroup$










      • 1




        $begingroup$
        you can also use FlattenAt: FlattenAt[g[f[a, b, c, d], e], 1] (+1)
        $endgroup$
        – kglr
        Aug 11 at 16:05














      Your Answer








      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%2f203569%2fchanging-number-of-arguments-to-a-function-in-secondary-evaluation%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      9













      $begingroup$

      From my own comment:



      You're mixing the concepts of pure functions (# and &) with replacement (/., ->, :>). Use slots with the first one, use patterns (Pattern in the documentation, x_ or x__ for example in usage) with the second.



      For this problem:



      f[a, b, c, d] /. f[x__] :> g[x, e]



      g[a, b, c, d, e]




      Note the use of __, which is two underscores: This is a pattern which grabs 1 or more elements (see also ___ which grabs 0 or more elements), and inserts them as a Sequence when used in replacement. Thus, it doesn't get inserted as a List.



      If you wanted to do this with pure functions, it becomes a bit more complicated:



      g[Sequence @@ #, e] &[f[a, b, c, d]]



      g[a, b, c, d, e]




      But note that this isn't dependent on its argument being in the form of f[...], it will replace any functional head. To avoid that requires conditionals of some variety, e.g:



      If[Head[#] === f, g[Sequence @@ #, e], Undefined] &[f[a, b, c, d]]





      share|improve this answer











      $endgroup$



















        9













        $begingroup$

        From my own comment:



        You're mixing the concepts of pure functions (# and &) with replacement (/., ->, :>). Use slots with the first one, use patterns (Pattern in the documentation, x_ or x__ for example in usage) with the second.



        For this problem:



        f[a, b, c, d] /. f[x__] :> g[x, e]



        g[a, b, c, d, e]




        Note the use of __, which is two underscores: This is a pattern which grabs 1 or more elements (see also ___ which grabs 0 or more elements), and inserts them as a Sequence when used in replacement. Thus, it doesn't get inserted as a List.



        If you wanted to do this with pure functions, it becomes a bit more complicated:



        g[Sequence @@ #, e] &[f[a, b, c, d]]



        g[a, b, c, d, e]




        But note that this isn't dependent on its argument being in the form of f[...], it will replace any functional head. To avoid that requires conditionals of some variety, e.g:



        If[Head[#] === f, g[Sequence @@ #, e], Undefined] &[f[a, b, c, d]]





        share|improve this answer











        $endgroup$

















          9














          9










          9







          $begingroup$

          From my own comment:



          You're mixing the concepts of pure functions (# and &) with replacement (/., ->, :>). Use slots with the first one, use patterns (Pattern in the documentation, x_ or x__ for example in usage) with the second.



          For this problem:



          f[a, b, c, d] /. f[x__] :> g[x, e]



          g[a, b, c, d, e]




          Note the use of __, which is two underscores: This is a pattern which grabs 1 or more elements (see also ___ which grabs 0 or more elements), and inserts them as a Sequence when used in replacement. Thus, it doesn't get inserted as a List.



          If you wanted to do this with pure functions, it becomes a bit more complicated:



          g[Sequence @@ #, e] &[f[a, b, c, d]]



          g[a, b, c, d, e]




          But note that this isn't dependent on its argument being in the form of f[...], it will replace any functional head. To avoid that requires conditionals of some variety, e.g:



          If[Head[#] === f, g[Sequence @@ #, e], Undefined] &[f[a, b, c, d]]





          share|improve this answer











          $endgroup$



          From my own comment:



          You're mixing the concepts of pure functions (# and &) with replacement (/., ->, :>). Use slots with the first one, use patterns (Pattern in the documentation, x_ or x__ for example in usage) with the second.



          For this problem:



          f[a, b, c, d] /. f[x__] :> g[x, e]



          g[a, b, c, d, e]




          Note the use of __, which is two underscores: This is a pattern which grabs 1 or more elements (see also ___ which grabs 0 or more elements), and inserts them as a Sequence when used in replacement. Thus, it doesn't get inserted as a List.



          If you wanted to do this with pure functions, it becomes a bit more complicated:



          g[Sequence @@ #, e] &[f[a, b, c, d]]



          g[a, b, c, d, e]




          But note that this isn't dependent on its argument being in the form of f[...], it will replace any functional head. To avoid that requires conditionals of some variety, e.g:



          If[Head[#] === f, g[Sequence @@ #, e], Undefined] &[f[a, b, c, d]]






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 10 at 18:23

























          answered Aug 10 at 18:12









          eyorbleeyorble

          6,6781 gold badge12 silver badges30 bronze badges




          6,6781 gold badge12 silver badges30 bronze badges


























              7













              $begingroup$

              You can also use Apply:



              g[##, e] & @@ f[a, b, c, d]



              g[a, b, c, d, e]




              or ReplaceAll with replacement rule f -> (g[##, e] &):



              f[a, b, c, d] /. f -> (g[##, e] &)



              g[a, b, c, d, e]







              share|improve this answer









              $endgroup$



















                7













                $begingroup$

                You can also use Apply:



                g[##, e] & @@ f[a, b, c, d]



                g[a, b, c, d, e]




                or ReplaceAll with replacement rule f -> (g[##, e] &):



                f[a, b, c, d] /. f -> (g[##, e] &)



                g[a, b, c, d, e]







                share|improve this answer









                $endgroup$

















                  7














                  7










                  7







                  $begingroup$

                  You can also use Apply:



                  g[##, e] & @@ f[a, b, c, d]



                  g[a, b, c, d, e]




                  or ReplaceAll with replacement rule f -> (g[##, e] &):



                  f[a, b, c, d] /. f -> (g[##, e] &)



                  g[a, b, c, d, e]







                  share|improve this answer









                  $endgroup$



                  You can also use Apply:



                  g[##, e] & @@ f[a, b, c, d]



                  g[a, b, c, d, e]




                  or ReplaceAll with replacement rule f -> (g[##, e] &):



                  f[a, b, c, d] /. f -> (g[##, e] &)



                  g[a, b, c, d, e]








                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 10 at 19:24









                  kglrkglr

                  213k10 gold badges243 silver badges487 bronze badges




                  213k10 gold badges243 silver badges487 bronze badges
























                      2













                      $begingroup$

                      There is yet another solution, using Flatten with Head f, namely



                      Flatten[g[f[a, b, c, d], e], 1, f]



                      g[a, b, c, d, e]




                      This assumes that g is not defined for two arguments, otherwise it will evaluate before Flatten has a chance so flatten the f. If that's not the case one can use Inactive on g, act with Flatten and then Activate again.



                      Activate[Flatten[Inactive[g][f[a, b, c, d], e], 1, f]]





                      share|improve this answer









                      $endgroup$










                      • 1




                        $begingroup$
                        you can also use FlattenAt: FlattenAt[g[f[a, b, c, d], e], 1] (+1)
                        $endgroup$
                        – kglr
                        Aug 11 at 16:05
















                      2













                      $begingroup$

                      There is yet another solution, using Flatten with Head f, namely



                      Flatten[g[f[a, b, c, d], e], 1, f]



                      g[a, b, c, d, e]




                      This assumes that g is not defined for two arguments, otherwise it will evaluate before Flatten has a chance so flatten the f. If that's not the case one can use Inactive on g, act with Flatten and then Activate again.



                      Activate[Flatten[Inactive[g][f[a, b, c, d], e], 1, f]]





                      share|improve this answer









                      $endgroup$










                      • 1




                        $begingroup$
                        you can also use FlattenAt: FlattenAt[g[f[a, b, c, d], e], 1] (+1)
                        $endgroup$
                        – kglr
                        Aug 11 at 16:05














                      2














                      2










                      2







                      $begingroup$

                      There is yet another solution, using Flatten with Head f, namely



                      Flatten[g[f[a, b, c, d], e], 1, f]



                      g[a, b, c, d, e]




                      This assumes that g is not defined for two arguments, otherwise it will evaluate before Flatten has a chance so flatten the f. If that's not the case one can use Inactive on g, act with Flatten and then Activate again.



                      Activate[Flatten[Inactive[g][f[a, b, c, d], e], 1, f]]





                      share|improve this answer









                      $endgroup$



                      There is yet another solution, using Flatten with Head f, namely



                      Flatten[g[f[a, b, c, d], e], 1, f]



                      g[a, b, c, d, e]




                      This assumes that g is not defined for two arguments, otherwise it will evaluate before Flatten has a chance so flatten the f. If that's not the case one can use Inactive on g, act with Flatten and then Activate again.



                      Activate[Flatten[Inactive[g][f[a, b, c, d], e], 1, f]]






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Aug 11 at 15:57









                      MannyCMannyC

                      3181 silver badge7 bronze badges




                      3181 silver badge7 bronze badges










                      • 1




                        $begingroup$
                        you can also use FlattenAt: FlattenAt[g[f[a, b, c, d], e], 1] (+1)
                        $endgroup$
                        – kglr
                        Aug 11 at 16:05













                      • 1




                        $begingroup$
                        you can also use FlattenAt: FlattenAt[g[f[a, b, c, d], e], 1] (+1)
                        $endgroup$
                        – kglr
                        Aug 11 at 16:05








                      1




                      1




                      $begingroup$
                      you can also use FlattenAt: FlattenAt[g[f[a, b, c, d], e], 1] (+1)
                      $endgroup$
                      – kglr
                      Aug 11 at 16:05





                      $begingroup$
                      you can also use FlattenAt: FlattenAt[g[f[a, b, c, d], e], 1] (+1)
                      $endgroup$
                      – kglr
                      Aug 11 at 16:05


















                      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%2f203569%2fchanging-number-of-arguments-to-a-function-in-secondary-evaluation%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

                      Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

                      Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

                      Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?