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;
$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/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
);
);
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,6781 gold badge12 silver badges30 bronze badges
6,6781 gold badge12 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
213k10 gold badges243 silver badges487 bronze badges
213k10 gold badges243 silver badges487 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