RE: Cases, Throw, and Catch
- To: mathgroup at smc.vnet.net
- Subject: [mg24208] RE: [mg24182] Cases, Throw, and Catch
- From: "David Park" <djmp at earthlink.net>
- Date: Fri, 30 Jun 2000 01:57:35 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Mark, I wonder if Mathematica is misleading you here. I don't think you can use Throw that way in a Cases statement. Mathematica is looking for a pattern there. When it finds Throw it just Throws whatever you told it to Throw. It probably doesn't even look at the list at all. How is it suppose to know what to look for? Catch @ Cases[ytab, Throw[{"Fooled Again!"}], Infinity, 1] // Timing {0. Second, {"Fooled Again!"}} David Park djmp at earthlink.net http://home.earthlink.net/~djmp/ > I just noticed that it is much faster to Throw and Catch from Cases than > to have Cases return the first match on its own. Does anyone know why > this is so and what, if anything, is to be learned? > > Here is the example: > > In[1]:= > xtab = Table[x, {10^6}]; > > In[2]:= > Cases[xtab, x, Infinity, 1] // Timing > > Out[2]= > {0.172 Second, {x}} > > In[3]:= > Catch @ Cases[xtab, Throw[{x}], Infinity, 1] // Timing > > Out[3]= > {0. Second, {x}} > > In[4]:= > ytab = Append[xtab, y]; > > In[5]:= > Cases[ytab, y, Infinity, 1] // Timing > > Out[5]= > {0.437 Second, {y}} > > In[6]:= > Catch @ Cases[ytab, Throw[{y}], Infinity, 1] // Timing > > Out[6]= > {0. Second, {y}} > > --Mark. > > Mark Fisher > Economic Advisor > Research Department > Federal Reserve Bank of Atlanta > Atlanta, GA 30303 > 404-521-8757 (voice) > 404-521-8810 (fax) > mark.fisher at atl.frb.org > > personal web page: www.markfisher.net > >