Pure function as a variable of another function

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












In a notebook, I have a function that basically needs to do something like calling a pure function within a function. I would say that what best describes would be an example like:



f[x_, func_: # &] := func[x]


The problem with this is that when I use it, for example just f[x] the output is f[x] or (#1)[x] and if I call f[x, (1 + #) &] the output is (1 + #1)[x]



Even worse, if, for some reason, I decide to change the definition of f to



f[x_, func_: Exp] := func[x]


and then change it back to the original definition, the output to f[x] will now forever be Exp[x].



So, is there a way to solve this problem so that with the first definition I get the result I'm waiting f[x] = x and f[x, (1 + #) &] = 1 + x?



Thanks for the help.










share|improve this question









New contributor




Argidore is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 3




    f[x_, func_: (# &)] := func[x] otherwise it is (func_: #) &
    – Kuba♦
    1 hour ago










  • Yeah, that seems to solve it! Thanks
    – Argidore
    1 hour ago










  • From all the stuff that I tried to find a solution, never thought it would be as simple as that
    – Argidore
    1 hour ago










  • @Argidore There exists a concept Precedence of operators. The problem arises from the fact that Function (&) has a rather low precedence. This can be seen by running this command: Precedence /@ Pattern, Optional, Function.
    – Î‘λέξανδρος Ζεγγ
    1 hour ago














up vote
2
down vote

favorite












In a notebook, I have a function that basically needs to do something like calling a pure function within a function. I would say that what best describes would be an example like:



f[x_, func_: # &] := func[x]


The problem with this is that when I use it, for example just f[x] the output is f[x] or (#1)[x] and if I call f[x, (1 + #) &] the output is (1 + #1)[x]



Even worse, if, for some reason, I decide to change the definition of f to



f[x_, func_: Exp] := func[x]


and then change it back to the original definition, the output to f[x] will now forever be Exp[x].



So, is there a way to solve this problem so that with the first definition I get the result I'm waiting f[x] = x and f[x, (1 + #) &] = 1 + x?



Thanks for the help.










share|improve this question









New contributor




Argidore is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 3




    f[x_, func_: (# &)] := func[x] otherwise it is (func_: #) &
    – Kuba♦
    1 hour ago










  • Yeah, that seems to solve it! Thanks
    – Argidore
    1 hour ago










  • From all the stuff that I tried to find a solution, never thought it would be as simple as that
    – Argidore
    1 hour ago










  • @Argidore There exists a concept Precedence of operators. The problem arises from the fact that Function (&) has a rather low precedence. This can be seen by running this command: Precedence /@ Pattern, Optional, Function.
    – Î‘λέξανδρος Ζεγγ
    1 hour ago












up vote
2
down vote

favorite









up vote
2
down vote

favorite











In a notebook, I have a function that basically needs to do something like calling a pure function within a function. I would say that what best describes would be an example like:



f[x_, func_: # &] := func[x]


The problem with this is that when I use it, for example just f[x] the output is f[x] or (#1)[x] and if I call f[x, (1 + #) &] the output is (1 + #1)[x]



Even worse, if, for some reason, I decide to change the definition of f to



f[x_, func_: Exp] := func[x]


and then change it back to the original definition, the output to f[x] will now forever be Exp[x].



So, is there a way to solve this problem so that with the first definition I get the result I'm waiting f[x] = x and f[x, (1 + #) &] = 1 + x?



Thanks for the help.










share|improve this question









New contributor




Argidore is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











In a notebook, I have a function that basically needs to do something like calling a pure function within a function. I would say that what best describes would be an example like:



f[x_, func_: # &] := func[x]


The problem with this is that when I use it, for example just f[x] the output is f[x] or (#1)[x] and if I call f[x, (1 + #) &] the output is (1 + #1)[x]



Even worse, if, for some reason, I decide to change the definition of f to



f[x_, func_: Exp] := func[x]


and then change it back to the original definition, the output to f[x] will now forever be Exp[x].



So, is there a way to solve this problem so that with the first definition I get the result I'm waiting f[x] = x and f[x, (1 + #) &] = 1 + x?



Thanks for the help.







function-construction pure-function






share|improve this question









New contributor




Argidore is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Argidore is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 1 hour ago









Αλέξανδρος Ζεγγ

2,9461827




2,9461827






New contributor




Argidore is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 hours ago









Argidore

111




111




New contributor




Argidore is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Argidore is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Argidore is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 3




    f[x_, func_: (# &)] := func[x] otherwise it is (func_: #) &
    – Kuba♦
    1 hour ago










  • Yeah, that seems to solve it! Thanks
    – Argidore
    1 hour ago










  • From all the stuff that I tried to find a solution, never thought it would be as simple as that
    – Argidore
    1 hour ago










  • @Argidore There exists a concept Precedence of operators. The problem arises from the fact that Function (&) has a rather low precedence. This can be seen by running this command: Precedence /@ Pattern, Optional, Function.
    – Î‘λέξανδρος Ζεγγ
    1 hour ago












  • 3




    f[x_, func_: (# &)] := func[x] otherwise it is (func_: #) &
    – Kuba♦
    1 hour ago










  • Yeah, that seems to solve it! Thanks
    – Argidore
    1 hour ago










  • From all the stuff that I tried to find a solution, never thought it would be as simple as that
    – Argidore
    1 hour ago










  • @Argidore There exists a concept Precedence of operators. The problem arises from the fact that Function (&) has a rather low precedence. This can be seen by running this command: Precedence /@ Pattern, Optional, Function.
    – Î‘λέξανδρος Ζεγγ
    1 hour ago







3




3




f[x_, func_: (# &)] := func[x] otherwise it is (func_: #) &
– Kuba♦
1 hour ago




f[x_, func_: (# &)] := func[x] otherwise it is (func_: #) &
– Kuba♦
1 hour ago












Yeah, that seems to solve it! Thanks
– Argidore
1 hour ago




Yeah, that seems to solve it! Thanks
– Argidore
1 hour ago












From all the stuff that I tried to find a solution, never thought it would be as simple as that
– Argidore
1 hour ago




From all the stuff that I tried to find a solution, never thought it would be as simple as that
– Argidore
1 hour ago












@Argidore There exists a concept Precedence of operators. The problem arises from the fact that Function (&) has a rather low precedence. This can be seen by running this command: Precedence /@ Pattern, Optional, Function.
– Î‘λέξανδρος Ζεγγ
1 hour ago




@Argidore There exists a concept Precedence of operators. The problem arises from the fact that Function (&) has a rather low precedence. This can be seen by running this command: Precedence /@ Pattern, Optional, Function.
– Î‘λέξανδρος Ζεγγ
1 hour ago










1 Answer
1






active

oldest

votes

















up vote
4
down vote













To specify values to a function's argument(s) which take effect when calling the function but no explicit values are going to be set to them, besides Optional (:), one can also use Default (.):



Default[f, 2] = #^2 &;

f[x_, func_.] := func[x]

f[a]



a^2



In this case, no bother with precedences of operators.






share|improve this answer






















    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',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    Argidore is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f184599%2fpure-function-as-a-variable-of-another-function%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    4
    down vote













    To specify values to a function's argument(s) which take effect when calling the function but no explicit values are going to be set to them, besides Optional (:), one can also use Default (.):



    Default[f, 2] = #^2 &;

    f[x_, func_.] := func[x]

    f[a]



    a^2



    In this case, no bother with precedences of operators.






    share|improve this answer


























      up vote
      4
      down vote













      To specify values to a function's argument(s) which take effect when calling the function but no explicit values are going to be set to them, besides Optional (:), one can also use Default (.):



      Default[f, 2] = #^2 &;

      f[x_, func_.] := func[x]

      f[a]



      a^2



      In this case, no bother with precedences of operators.






      share|improve this answer
























        up vote
        4
        down vote










        up vote
        4
        down vote









        To specify values to a function's argument(s) which take effect when calling the function but no explicit values are going to be set to them, besides Optional (:), one can also use Default (.):



        Default[f, 2] = #^2 &;

        f[x_, func_.] := func[x]

        f[a]



        a^2



        In this case, no bother with precedences of operators.






        share|improve this answer














        To specify values to a function's argument(s) which take effect when calling the function but no explicit values are going to be set to them, besides Optional (:), one can also use Default (.):



        Default[f, 2] = #^2 &;

        f[x_, func_.] := func[x]

        f[a]



        a^2



        In this case, no bother with precedences of operators.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 1 hour ago

























        answered 1 hour ago









        Αλέξανδρος Ζεγγ

        2,9461827




        2,9461827




















            Argidore is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            Argidore is a new contributor. Be nice, and check out our Code of Conduct.












            Argidore is a new contributor. Be nice, and check out our Code of Conduct.











            Argidore is a new contributor. Be nice, and check out our Code of Conduct.













             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f184599%2fpure-function-as-a-variable-of-another-function%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            Confectionery