Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

19
  • 3
    $\begingroup$ plt //. _[___, x_Line, ___] :> x $\endgroup$ Commented May 27, 2014 at 19:35
  • 1
    $\begingroup$ @saturasl @Kuba's way is how I would do it with patterns, but I understand what you're asking. I don't know how to achieve that with /. (I'll mull it over.) But concerning your original question, I don't think that is how Cases is implemented. My guess is the algorithm looks more like Reap[Scan[If[MatchQ[#, _Line], Sow[#]] &, plt, \[Infinity]]]. $\endgroup$ Commented May 27, 2014 at 19:58
  • 6
    $\begingroup$ It is appropriate to point out an important difference between Cases and Replace(All) here. The former walks the expression tree from inside out (leaves first, root last), while ReplaceAll goes the other way: outside first, then the leaves of the tree last. Yes, one of them can emulate the other, but this difference will always persist. *Also: Your question implies that ReplaceAll is somehow more fundamental ("generic pattern matching") than Cases. I wouldn't say this is the case and I'm pretty sure internally one isn't implemented in terms of the other. $\endgroup$ Commented May 27, 2014 at 20:44
  • 1
    $\begingroup$ The most effective method is to use Cases which is what I use. A Graphics object describes a state machine, and if you want something that captures the structure, use a parser. But, usually that is overkill, so stick with Cases. $\endgroup$ Commented May 28, 2014 at 0:41
  • 1
    $\begingroup$ Related to Szabolcs's comment, and possibly of interest: (9209), (9233) $\endgroup$ Commented May 28, 2014 at 6:44