Pure function as a variable of another function
Clash 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.
function-construction pure-function
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.
add a comment |Â
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.
function-construction pure-function
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 conceptPrecedence
of operators. The problem arises from the fact thatFunction (&)
has a rather low precedence. This can be seen by running this command:Precedence /@ Pattern, Optional, Function
.
– Î‘λÎÂξανδÃÂο Ζεγγ
1 hour ago
add a comment |Â
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.
function-construction pure-function
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
function-construction pure-function
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.
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 conceptPrecedence
of operators. The problem arises from the fact thatFunction (&)
has a rather low precedence. This can be seen by running this command:Precedence /@ Pattern, Optional, Function
.
– Î‘λÎÂξανδÃÂο Ζεγγ
1 hour ago
add a comment |Â
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 conceptPrecedence
of operators. The problem arises from the fact thatFunction (&)
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
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
edited 1 hour ago
answered 1 hour ago


ΑλÎÂξανδÃÂο Ζεγγ
2,9461827
2,9461827
add a comment |Â
add a comment |Â
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.
Argidore is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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 thatFunction (&)
has a rather low precedence. This can be seen by running this command:Precedence /@ Pattern, Optional, Function
.– Î‘λÎÂξανδÃÂο Ζεγγ
1 hour ago