Re: Early out with Map?
- To: mathgroup at smc.vnet.net
- Subject: [mg16047] Re: [mg16036] Early out with Map?
- From: "Carl K.Woll" <carlw at fermi.phys.washington.edu>
- Date: Sun, 21 Feb 1999 00:15:20 -0500
- Organization: Department of Physics
- References: <199902200752.CAA24451@smc.vnet.net.>
- Sender: owner-wri-mathgroup at wolfram.com
Oscar, Why don't you use the function Position[]? For example, Position[children, a_ /; testChild[a], 1, 1] will return either the position of the first element of children which satisfies testChild, or else it will return an empty list if no elements of children satisfy the test. Turning this into a True False statement isn't difficult. Carl Woll Dept of Physics U of Washington Oscar Stiffelman wrote: > Hi, > > Is it possible to interrupt Map before it reaches then end of the list? > > For example, how would I convert the following from procedural to > functional? > > (* This is the procedural version *) > foundSolution = False; > For[i =1, i <= numChildren && ! foundSolution, i++, > If[testChild[children[[i]]], > foundSolution = True; > ]; > ]; > foundSolution > > (* > * This keeps going until it reaches the end of the list. > * I would prefer to return as soon as testChild returns True > *) > Or @@ Map[testChild, children] > > (* > * This does not actually return > *) > Map[If[testChild[#], Return[True], False]&, children]
- References:
- Early out with Map?
- From: "Oscar Stiffelman" <ostiffel@leland.stanford.edu>
- Early out with Map?