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 PrecisionFunctions that remember some arguments while not remembering other arguments
How does Facebook track your browsing without third party cookies?
Minimum age of קידושין for a female?
Can the jeopardy of being judged be fought against in court?
Where do I put nobles/royalty in a science fiction military?
Can a Tiefling be born from two human parents?
Two super-button calculator
Produce the random variable for an asset from a uniformly distributed random varible
How do gelatinous cubes reproduce?
Minipage misalign horizontally 2 long side-by-side tables and cause them to overlap, even though plenty of space free
Plot the Pascalian triangle
Weird Power Outage in Certain Rooms- Condominium
Responding to social invitations with autism
Why does "Endgame" have a different time travel theory?
In Flanders Fields
What is the last point where one can throw away fruits if one has indicated "not bringing any fruit" on the US customs form when flying to the US?
How to react to unfair reviewer comments?
Why don't we shield existing CPUs from radiation instead of designing new ones?
What's the difference between words "tongue" and "lingua"?
A feasible and efficient method of fast global travel?
Stop autocorrect from autocorrecting a word even if I re-type it after it has been autocorrected
What color is a wolf's coat?
Intersection of four circles
A major investor asked me (the software lead) whether the board should fire my boss (the Co-Founder and CTO). What should I do?
How to translate "cocotte en papier" in English?
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 PrecisionFunctions 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;
.everyonelovesstackoverflowposition:absolute;height:1px;width:1px;opacity:0;top:0;left:0;pointer-events:none;
$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]
functions argument-patterns
$endgroup$
add a comment
|
$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]
functions argument-patterns
$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_
orx__
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
add a comment
|
$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]
functions argument-patterns
$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
functions argument-patterns
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_
orx__
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
add a comment
|
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_
orx__
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
add a comment
|
3 Answers
3
active
oldest
votes
$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]]
$endgroup$
add a comment
|
$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]
$endgroup$
add a comment
|
$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]]
$endgroup$
1
$begingroup$
you can also useFlattenAt
:FlattenAt[g[f[a, b, c, d], e], 1]
(+1)
$endgroup$
– kglr
Aug 11 at 16:05
add a comment
|
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/4.0/"u003ecc by-sa 4.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
);
);
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
Required, but never shown
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
$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]]
$endgroup$
add a comment
|
$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]]
$endgroup$
add a comment
|
$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]]
$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]]
edited Aug 10 at 18:23
answered Aug 10 at 18:12
eyorbleeyorble
6,7031 gold badge13 silver badges30 bronze badges
6,7031 gold badge13 silver badges30 bronze badges
add a comment
|
add a comment
|
$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]
$endgroup$
add a comment
|
$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]
$endgroup$
add a comment
|
$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]
$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]
answered Aug 10 at 19:24
kglrkglr
226k10 gold badges256 silver badges514 bronze badges
226k10 gold badges256 silver badges514 bronze badges
add a comment
|
add a comment
|
$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]]
$endgroup$
1
$begingroup$
you can also useFlattenAt
:FlattenAt[g[f[a, b, c, d], e], 1]
(+1)
$endgroup$
– kglr
Aug 11 at 16:05
add a comment
|
$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]]
$endgroup$
1
$begingroup$
you can also useFlattenAt
:FlattenAt[g[f[a, b, c, d], e], 1]
(+1)
$endgroup$
– kglr
Aug 11 at 16:05
add a comment
|
$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]]
$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]]
answered Aug 11 at 15:57
MannyCMannyC
3181 silver badge7 bronze badges
3181 silver badge7 bronze badges
1
$begingroup$
you can also useFlattenAt
:FlattenAt[g[f[a, b, c, d], e], 1]
(+1)
$endgroup$
– kglr
Aug 11 at 16:05
add a comment
|
1
$begingroup$
you can also useFlattenAt
: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
add a comment
|
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.
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
Required, but never shown
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
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
Required, but never shown
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
Required, but never shown
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
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
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_
orx__
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