Timeline for Emulating Sequence with my own function
Current License: CC BY-SA 3.0
14 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Aug 31, 2012 at 9:12 | vote | accept | celtschk | ||
Jun 19, 2012 at 16:03 | comment | added | Rojo |
I agree 100% now. Answering your previous comment, I think of replace and family as something that works in 3 steps: match, build, replace. In the first 2, there are clearly deep internal things going on since __ matches "real" sequences. Same for the building. But the expression built has to be a MMA expression.
|
|
Jun 19, 2012 at 15:55 | comment | added | Leonid Shifrin | @Rojo Ok, you may or may not be right. Pattern-matcher is sufficently separated from evaluation process so that this could be true, but also sufficiently integrated into it that it could be not. This is really a question of how deep the pattern-matcher is integrated with the evaluator, in this particular respect. | |
Jun 19, 2012 at 15:47 | comment | added | Rojo |
What would you have Head@Unevaluated[1,2] return, or y[x__] := x; y[2, 3] or {1, 2} /. {l__} :> l ? Sequence is the standard way in MMA to represent sequences of arguments, and I see no other reasonable alternative. However, where there are alternatives such as in {1, 2} /. {l__} :> HoldComplete[l] , l__ is shown to represent the "real" sequence and not a _Sequence expression
|
|
Jun 19, 2012 at 15:05 | comment | added | Leonid Shifrin |
@Rojo sorry, have you seen my last comment? If so, please tell me, how else than the way I did can you explain the result of this: HoldComplete[1, 2, ff[3, 4, 5], 6, 7] /. ff[y__] :> y . By your (or Mr.Wizard's) logic, we should get here HoldComplete[1,2,3,4,5,6,7] , while we get HoldComplete[1,2,Sequence[3,4,5],6,7] . I'd rather think that his examples reveal some inconsistency in the pattern-matching / destructuring, than the other way around.
|
|
Jun 19, 2012 at 14:33 | comment | added | Rojo | Perhaps there's another internal sequence-like thing used that evaluates to Sequence in the end, but flattens itself out even in HoldComplete. No idea, but I think we are all guessing big about internals that probably won't ever be relevant :P | |
Jun 19, 2012 at 14:32 | comment | added | Rojo |
I almost second @Mr.Wizard except for the part where Unevaluated has a role (which I just don't have any hunch at all). I don't think Leonid's example proves that in his solution there's an internal use of Sequence . I imagine that when the replacer builds the expression :>rhs, if it a sequence, it has no other choice than to wrapping it in Sequence before putting it wherever it should be put. But that doesn't apply to his proposed solution, it's not a f[sth___]:=sth definition but something like ff[2, 3] /. ff[x___] :> HoldComplete[x] which seems to avoid Sequence
|
|
Jun 19, 2012 at 11:01 | comment | added | Leonid Shifrin |
@Mr.Wizard Ok, check this out: HoldComplete[1, 2, ff[3, 4, 5], 6, 7] /. ff[y__] :> y .
|
|
Jun 19, 2012 at 7:53 | comment | added | Leonid Shifrin |
@Mr.Wizard Well, you may be right, but I feel that Unevaluated should have nothing to do with that, it has a different purpose. OTOH, it is very natural that __ and ## use Sequence internally, since it is the same mechanism behind al of them. Perhaps, a better statement would be that all sequences are using the same underlying mechanism. I also second Szabolcs, I seem to remember cases where Sequence was appearing in the evaluation of __ and ## - related code. Will also try to find those cases.
|
|
Jun 19, 2012 at 7:02 | comment | added | Szabolcs |
@Mr.Wizard I'd have to sit down and experiment a bit to find where exactly, but I've seen __ and## transform into Sequence before. I think they do make use of Sequence at some point during their evaluation.
|
|
Jun 19, 2012 at 1:30 | comment | added | Mr.Wizard |
Leonid, I hate to disappoint but I think you're getting a false result in your test. I don't think ___ or ## are implemented using Sequence though they are of course analogous. In my opinion you cannot "catch" the y on the RHS in some intermediate form using Unevaluated . You are instead seeing this: Head@Unevaluated[1, 2, 3] yields Sequence ; it is an effect of Unevaluated .
|
|
Jun 18, 2012 at 21:00 | history | edited | Leonid Shifrin | CC BY-SA 3.0 |
Added a note on Sequence
|
Jun 18, 2012 at 20:43 | history | edited | Leonid Shifrin | CC BY-SA 3.0 |
added 251 characters in body
|
Jun 18, 2012 at 20:34 | history | answered | Leonid Shifrin | CC BY-SA 3.0 |