How is Sequence[Range[5], 3, 2] // Partition@# & parsed?
$begingroup$
For learning purposes, I want to rewrite the following
Partition@Sequence[Range[5],3,2]
{{1,2,3},{3,4,5}}
into another one
Sequence[Range[5], 3, 2] // Partition@# &
Partition::argbu: Partition called with 1 argument; between 2 and 6 arguments are expected.
Partition[{1,2,3,4,5}]
However, the latter does not produce what the former does.
Question
What do I have to do to fix the latter to produce what the former does?
expression-manipulation
$endgroup$
add a comment |
$begingroup$
For learning purposes, I want to rewrite the following
Partition@Sequence[Range[5],3,2]
{{1,2,3},{3,4,5}}
into another one
Sequence[Range[5], 3, 2] // Partition@# &
Partition::argbu: Partition called with 1 argument; between 2 and 6 arguments are expected.
Partition[{1,2,3,4,5}]
However, the latter does not produce what the former does.
Question
What do I have to do to fix the latter to produce what the former does?
expression-manipulation
$endgroup$
add a comment |
$begingroup$
For learning purposes, I want to rewrite the following
Partition@Sequence[Range[5],3,2]
{{1,2,3},{3,4,5}}
into another one
Sequence[Range[5], 3, 2] // Partition@# &
Partition::argbu: Partition called with 1 argument; between 2 and 6 arguments are expected.
Partition[{1,2,3,4,5}]
However, the latter does not produce what the former does.
Question
What do I have to do to fix the latter to produce what the former does?
expression-manipulation
$endgroup$
For learning purposes, I want to rewrite the following
Partition@Sequence[Range[5],3,2]
{{1,2,3},{3,4,5}}
into another one
Sequence[Range[5], 3, 2] // Partition@# &
Partition::argbu: Partition called with 1 argument; between 2 and 6 arguments are expected.
Partition[{1,2,3,4,5}]
However, the latter does not produce what the former does.
Question
What do I have to do to fix the latter to produce what the former does?
expression-manipulation
expression-manipulation
asked Feb 11 at 12:05
The Inventor of GodThe Inventor of God
205111
205111
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
$begingroup$
This is not about parsing, it's about evaluation.
Consider
f[Sequence[1,2]]
which evaluates to f[1,2]
, and compare with
f[#]&[Sequence[1,2]]
which first evaluates to
f[#]&[1,2]
then to
f[1]
because only #1
(i.e. "#
") appears in the pure function and #2
is discarded.
You could use ##
instead. f[##]&[1, 2]
evaluates to f[1,2]
.
$endgroup$
$begingroup$
Is it possible to hold theSequence
to prevent it from splicing inSequence[Range[5], 3, 2] // Partition@# &
? I just want to elaborate other methods if any.
$endgroup$
– The Inventor of God
Feb 11 at 13:32
1
$begingroup$
@ArtificialStupidity Add theSequenceHold
or other appropriate attribute to your pure function.Function[x, Partition[x], SequenceHold][Sequence[Range[5], 3, 2]]
. Or useUnevaluated
:Partition[#] &[Unevaluated@Sequence[Range[5], 3, 2]]
$endgroup$
– Szabolcs
Feb 11 at 13:59
$begingroup$
Thank you. I loveUnevaluated
.
$endgroup$
– The Inventor of God
Feb 11 at 14:11
add a comment |
$begingroup$
In order to observe the evaluation, compare the output of
TracePrint[Sequence[Range[5], 3, 2] // Partition@# &]
with that of the original,
TracePrint[Partition@Sequence[Range[5], 3, 2]]
It becomes clear that there's a term (Partition[#1]&)[{1,2,3,4,5},3,2]
appearing, which then becomes Partition[{1,2,3,4,5}]
, which is not what's desired (Partition
needs to be given more than just the first argument #1
); the original instead does Partition[{1,2,3,4,5},3,2]
.
As @szabolcs says, replacing # with ## does the trick:
TracePrint[Sequence[Range[5], 3, 2] // Partition@## &]
gives the right answer.
$endgroup$
add a comment |
$begingroup$
For longer chains of operations, and if the application will allow, it may be better to avoid trying to pass sequences through a pipeline. The issue is that sequences will, in general, require special handling at each step. On the other hand, lists pass through pipelines easily and can be converted to sequences as necessary using Apply
or ##
-- typically only on the terminal step.
Consider:
{Range[5], 3, 2} // Apply[Partition]
or, with a couple of additional steps:
{2, x, Range[5]} // ReplaceAll[x -> 3] // Reverse // Apply[Partition]
The latter example would be quite awkward to express if a sequence were being passed through the pipeline. Something like:
Sequence[2, x, Range[5]] //
Sequence @@ ReplaceAll[{##}, x -> 3] & //
Sequence @@ Reverse[{##}] & //
Partition[##] &
Notice how the sequence was temporarily converted to a list within each step, only to be converted back to a sequence afterwards. It is simpler to just pass lists through all the way until the end.
$endgroup$
$begingroup$
I like infix, for example,Sequence[a, b]~F~Sequence[c, d]
that is only possible withSequence
I think.
$endgroup$
– The Inventor of God
Feb 11 at 16:32
add a comment |
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',
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%2f191297%2fhow-is-sequencerange5-3-2-partition-parsed%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$
This is not about parsing, it's about evaluation.
Consider
f[Sequence[1,2]]
which evaluates to f[1,2]
, and compare with
f[#]&[Sequence[1,2]]
which first evaluates to
f[#]&[1,2]
then to
f[1]
because only #1
(i.e. "#
") appears in the pure function and #2
is discarded.
You could use ##
instead. f[##]&[1, 2]
evaluates to f[1,2]
.
$endgroup$
$begingroup$
Is it possible to hold theSequence
to prevent it from splicing inSequence[Range[5], 3, 2] // Partition@# &
? I just want to elaborate other methods if any.
$endgroup$
– The Inventor of God
Feb 11 at 13:32
1
$begingroup$
@ArtificialStupidity Add theSequenceHold
or other appropriate attribute to your pure function.Function[x, Partition[x], SequenceHold][Sequence[Range[5], 3, 2]]
. Or useUnevaluated
:Partition[#] &[Unevaluated@Sequence[Range[5], 3, 2]]
$endgroup$
– Szabolcs
Feb 11 at 13:59
$begingroup$
Thank you. I loveUnevaluated
.
$endgroup$
– The Inventor of God
Feb 11 at 14:11
add a comment |
$begingroup$
This is not about parsing, it's about evaluation.
Consider
f[Sequence[1,2]]
which evaluates to f[1,2]
, and compare with
f[#]&[Sequence[1,2]]
which first evaluates to
f[#]&[1,2]
then to
f[1]
because only #1
(i.e. "#
") appears in the pure function and #2
is discarded.
You could use ##
instead. f[##]&[1, 2]
evaluates to f[1,2]
.
$endgroup$
$begingroup$
Is it possible to hold theSequence
to prevent it from splicing inSequence[Range[5], 3, 2] // Partition@# &
? I just want to elaborate other methods if any.
$endgroup$
– The Inventor of God
Feb 11 at 13:32
1
$begingroup$
@ArtificialStupidity Add theSequenceHold
or other appropriate attribute to your pure function.Function[x, Partition[x], SequenceHold][Sequence[Range[5], 3, 2]]
. Or useUnevaluated
:Partition[#] &[Unevaluated@Sequence[Range[5], 3, 2]]
$endgroup$
– Szabolcs
Feb 11 at 13:59
$begingroup$
Thank you. I loveUnevaluated
.
$endgroup$
– The Inventor of God
Feb 11 at 14:11
add a comment |
$begingroup$
This is not about parsing, it's about evaluation.
Consider
f[Sequence[1,2]]
which evaluates to f[1,2]
, and compare with
f[#]&[Sequence[1,2]]
which first evaluates to
f[#]&[1,2]
then to
f[1]
because only #1
(i.e. "#
") appears in the pure function and #2
is discarded.
You could use ##
instead. f[##]&[1, 2]
evaluates to f[1,2]
.
$endgroup$
This is not about parsing, it's about evaluation.
Consider
f[Sequence[1,2]]
which evaluates to f[1,2]
, and compare with
f[#]&[Sequence[1,2]]
which first evaluates to
f[#]&[1,2]
then to
f[1]
because only #1
(i.e. "#
") appears in the pure function and #2
is discarded.
You could use ##
instead. f[##]&[1, 2]
evaluates to f[1,2]
.
answered Feb 11 at 12:12
SzabolcsSzabolcs
162k14441939
162k14441939
$begingroup$
Is it possible to hold theSequence
to prevent it from splicing inSequence[Range[5], 3, 2] // Partition@# &
? I just want to elaborate other methods if any.
$endgroup$
– The Inventor of God
Feb 11 at 13:32
1
$begingroup$
@ArtificialStupidity Add theSequenceHold
or other appropriate attribute to your pure function.Function[x, Partition[x], SequenceHold][Sequence[Range[5], 3, 2]]
. Or useUnevaluated
:Partition[#] &[Unevaluated@Sequence[Range[5], 3, 2]]
$endgroup$
– Szabolcs
Feb 11 at 13:59
$begingroup$
Thank you. I loveUnevaluated
.
$endgroup$
– The Inventor of God
Feb 11 at 14:11
add a comment |
$begingroup$
Is it possible to hold theSequence
to prevent it from splicing inSequence[Range[5], 3, 2] // Partition@# &
? I just want to elaborate other methods if any.
$endgroup$
– The Inventor of God
Feb 11 at 13:32
1
$begingroup$
@ArtificialStupidity Add theSequenceHold
or other appropriate attribute to your pure function.Function[x, Partition[x], SequenceHold][Sequence[Range[5], 3, 2]]
. Or useUnevaluated
:Partition[#] &[Unevaluated@Sequence[Range[5], 3, 2]]
$endgroup$
– Szabolcs
Feb 11 at 13:59
$begingroup$
Thank you. I loveUnevaluated
.
$endgroup$
– The Inventor of God
Feb 11 at 14:11
$begingroup$
Is it possible to hold the
Sequence
to prevent it from splicing in Sequence[Range[5], 3, 2] // Partition@# &
? I just want to elaborate other methods if any.$endgroup$
– The Inventor of God
Feb 11 at 13:32
$begingroup$
Is it possible to hold the
Sequence
to prevent it from splicing in Sequence[Range[5], 3, 2] // Partition@# &
? I just want to elaborate other methods if any.$endgroup$
– The Inventor of God
Feb 11 at 13:32
1
1
$begingroup$
@ArtificialStupidity Add the
SequenceHold
or other appropriate attribute to your pure function. Function[x, Partition[x], SequenceHold][Sequence[Range[5], 3, 2]]
. Or use Unevaluated
: Partition[#] &[Unevaluated@Sequence[Range[5], 3, 2]]
$endgroup$
– Szabolcs
Feb 11 at 13:59
$begingroup$
@ArtificialStupidity Add the
SequenceHold
or other appropriate attribute to your pure function. Function[x, Partition[x], SequenceHold][Sequence[Range[5], 3, 2]]
. Or use Unevaluated
: Partition[#] &[Unevaluated@Sequence[Range[5], 3, 2]]
$endgroup$
– Szabolcs
Feb 11 at 13:59
$begingroup$
Thank you. I love
Unevaluated
.$endgroup$
– The Inventor of God
Feb 11 at 14:11
$begingroup$
Thank you. I love
Unevaluated
.$endgroup$
– The Inventor of God
Feb 11 at 14:11
add a comment |
$begingroup$
In order to observe the evaluation, compare the output of
TracePrint[Sequence[Range[5], 3, 2] // Partition@# &]
with that of the original,
TracePrint[Partition@Sequence[Range[5], 3, 2]]
It becomes clear that there's a term (Partition[#1]&)[{1,2,3,4,5},3,2]
appearing, which then becomes Partition[{1,2,3,4,5}]
, which is not what's desired (Partition
needs to be given more than just the first argument #1
); the original instead does Partition[{1,2,3,4,5},3,2]
.
As @szabolcs says, replacing # with ## does the trick:
TracePrint[Sequence[Range[5], 3, 2] // Partition@## &]
gives the right answer.
$endgroup$
add a comment |
$begingroup$
In order to observe the evaluation, compare the output of
TracePrint[Sequence[Range[5], 3, 2] // Partition@# &]
with that of the original,
TracePrint[Partition@Sequence[Range[5], 3, 2]]
It becomes clear that there's a term (Partition[#1]&)[{1,2,3,4,5},3,2]
appearing, which then becomes Partition[{1,2,3,4,5}]
, which is not what's desired (Partition
needs to be given more than just the first argument #1
); the original instead does Partition[{1,2,3,4,5},3,2]
.
As @szabolcs says, replacing # with ## does the trick:
TracePrint[Sequence[Range[5], 3, 2] // Partition@## &]
gives the right answer.
$endgroup$
add a comment |
$begingroup$
In order to observe the evaluation, compare the output of
TracePrint[Sequence[Range[5], 3, 2] // Partition@# &]
with that of the original,
TracePrint[Partition@Sequence[Range[5], 3, 2]]
It becomes clear that there's a term (Partition[#1]&)[{1,2,3,4,5},3,2]
appearing, which then becomes Partition[{1,2,3,4,5}]
, which is not what's desired (Partition
needs to be given more than just the first argument #1
); the original instead does Partition[{1,2,3,4,5},3,2]
.
As @szabolcs says, replacing # with ## does the trick:
TracePrint[Sequence[Range[5], 3, 2] // Partition@## &]
gives the right answer.
$endgroup$
In order to observe the evaluation, compare the output of
TracePrint[Sequence[Range[5], 3, 2] // Partition@# &]
with that of the original,
TracePrint[Partition@Sequence[Range[5], 3, 2]]
It becomes clear that there's a term (Partition[#1]&)[{1,2,3,4,5},3,2]
appearing, which then becomes Partition[{1,2,3,4,5}]
, which is not what's desired (Partition
needs to be given more than just the first argument #1
); the original instead does Partition[{1,2,3,4,5},3,2]
.
As @szabolcs says, replacing # with ## does the trick:
TracePrint[Sequence[Range[5], 3, 2] // Partition@## &]
gives the right answer.
edited Feb 11 at 15:27
answered Feb 11 at 13:04
RomanRoman
2,950717
2,950717
add a comment |
add a comment |
$begingroup$
For longer chains of operations, and if the application will allow, it may be better to avoid trying to pass sequences through a pipeline. The issue is that sequences will, in general, require special handling at each step. On the other hand, lists pass through pipelines easily and can be converted to sequences as necessary using Apply
or ##
-- typically only on the terminal step.
Consider:
{Range[5], 3, 2} // Apply[Partition]
or, with a couple of additional steps:
{2, x, Range[5]} // ReplaceAll[x -> 3] // Reverse // Apply[Partition]
The latter example would be quite awkward to express if a sequence were being passed through the pipeline. Something like:
Sequence[2, x, Range[5]] //
Sequence @@ ReplaceAll[{##}, x -> 3] & //
Sequence @@ Reverse[{##}] & //
Partition[##] &
Notice how the sequence was temporarily converted to a list within each step, only to be converted back to a sequence afterwards. It is simpler to just pass lists through all the way until the end.
$endgroup$
$begingroup$
I like infix, for example,Sequence[a, b]~F~Sequence[c, d]
that is only possible withSequence
I think.
$endgroup$
– The Inventor of God
Feb 11 at 16:32
add a comment |
$begingroup$
For longer chains of operations, and if the application will allow, it may be better to avoid trying to pass sequences through a pipeline. The issue is that sequences will, in general, require special handling at each step. On the other hand, lists pass through pipelines easily and can be converted to sequences as necessary using Apply
or ##
-- typically only on the terminal step.
Consider:
{Range[5], 3, 2} // Apply[Partition]
or, with a couple of additional steps:
{2, x, Range[5]} // ReplaceAll[x -> 3] // Reverse // Apply[Partition]
The latter example would be quite awkward to express if a sequence were being passed through the pipeline. Something like:
Sequence[2, x, Range[5]] //
Sequence @@ ReplaceAll[{##}, x -> 3] & //
Sequence @@ Reverse[{##}] & //
Partition[##] &
Notice how the sequence was temporarily converted to a list within each step, only to be converted back to a sequence afterwards. It is simpler to just pass lists through all the way until the end.
$endgroup$
$begingroup$
I like infix, for example,Sequence[a, b]~F~Sequence[c, d]
that is only possible withSequence
I think.
$endgroup$
– The Inventor of God
Feb 11 at 16:32
add a comment |
$begingroup$
For longer chains of operations, and if the application will allow, it may be better to avoid trying to pass sequences through a pipeline. The issue is that sequences will, in general, require special handling at each step. On the other hand, lists pass through pipelines easily and can be converted to sequences as necessary using Apply
or ##
-- typically only on the terminal step.
Consider:
{Range[5], 3, 2} // Apply[Partition]
or, with a couple of additional steps:
{2, x, Range[5]} // ReplaceAll[x -> 3] // Reverse // Apply[Partition]
The latter example would be quite awkward to express if a sequence were being passed through the pipeline. Something like:
Sequence[2, x, Range[5]] //
Sequence @@ ReplaceAll[{##}, x -> 3] & //
Sequence @@ Reverse[{##}] & //
Partition[##] &
Notice how the sequence was temporarily converted to a list within each step, only to be converted back to a sequence afterwards. It is simpler to just pass lists through all the way until the end.
$endgroup$
For longer chains of operations, and if the application will allow, it may be better to avoid trying to pass sequences through a pipeline. The issue is that sequences will, in general, require special handling at each step. On the other hand, lists pass through pipelines easily and can be converted to sequences as necessary using Apply
or ##
-- typically only on the terminal step.
Consider:
{Range[5], 3, 2} // Apply[Partition]
or, with a couple of additional steps:
{2, x, Range[5]} // ReplaceAll[x -> 3] // Reverse // Apply[Partition]
The latter example would be quite awkward to express if a sequence were being passed through the pipeline. Something like:
Sequence[2, x, Range[5]] //
Sequence @@ ReplaceAll[{##}, x -> 3] & //
Sequence @@ Reverse[{##}] & //
Partition[##] &
Notice how the sequence was temporarily converted to a list within each step, only to be converted back to a sequence afterwards. It is simpler to just pass lists through all the way until the end.
answered Feb 11 at 15:56
WReachWReach
53.3k2114212
53.3k2114212
$begingroup$
I like infix, for example,Sequence[a, b]~F~Sequence[c, d]
that is only possible withSequence
I think.
$endgroup$
– The Inventor of God
Feb 11 at 16:32
add a comment |
$begingroup$
I like infix, for example,Sequence[a, b]~F~Sequence[c, d]
that is only possible withSequence
I think.
$endgroup$
– The Inventor of God
Feb 11 at 16:32
$begingroup$
I like infix, for example,
Sequence[a, b]~F~Sequence[c, d]
that is only possible with Sequence
I think.$endgroup$
– The Inventor of God
Feb 11 at 16:32
$begingroup$
I like infix, for example,
Sequence[a, b]~F~Sequence[c, d]
that is only possible with Sequence
I think.$endgroup$
– The Inventor of God
Feb 11 at 16:32
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%2f191297%2fhow-is-sequencerange5-3-2-partition-parsed%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