I just considered if/how one could implement Sequence
in Mathematica if it were not predefined. It turned out that the following simple definition has in all my tests exactly the right behaviour:
myseq /: f_[x___, myseq[y___], z___] := f[x, y, z]
Now my question: Does this already correctly reproduce the full behaviour of Sequence
, or is there something Sequence
does but myseq
doesn't which I missed in my tests?
Here's what I tested:
foo[myseq[a, b]]
(*
==> foo[a, b]
*)
Hold[mysec[a,b]]
(*
==> Hold[a, b]
*)
HoldComplete[myseq[a,b]]
(*
HoldComplete[myseq[a, b]]
*)
Hold[f[myseq[a,b]]]
(*
==> Hold[f[myseq[a, b]]]
*)
f[myseq[myseq[a,b],c,d],e,myseq[f,g,myseq[]]]
(*
==> f[a, b, c, d, e, f, g]
*)
myseq
fails is for something like{1, 2, 3} /. 2 -> myseq[a, b]
. $\endgroup$t = {{a, b}, {c, d}, f}; myseq @@t
fails $\endgroup$SequenceHold
likeSequnce
does. $\endgroup$myseq[1,2]
does the same. It may also be due to the lack ofSequenceHold
support. EDIT: it's not. Support can be added withmyseq /: f_[x___, myseq[y___], z___] /; FreeQ[Attributes[f], SequenceHold] := f[x, y, z]
$\endgroup$