RE: Early out with Map?
- To: mathgroup at smc.vnet.net
- Subject: [mg16041] RE: [mg16036] Early out with Map?
- From: "Jean-Marie THOMAS" <jmthomas at agat.net>
- Date: Sun, 21 Feb 1999 00:15:17 -0500
- Sender: owner-wri-mathgroup at wolfram.com
You can use Catch and Throw: myList=Range[10] myFunction:=If[#===7,Throw["out"]]& Catch[myFunction/@myList] Tracing Catch[myFunction/@myList] will show you myFunction is not evaluated for arguments after 7. Hope this helps, **************************************** Jean-Marie THOMAS mailto:jmthomas at agat.net Conseil et Audit en Ingenierie de Calcul Strasbourg, France http://www.agat.net **************************************** -----Original Message----- From: Oscar Stiffelman [mailto:ostiffel at leland.stanford.edu] To: mathgroup at smc.vnet.net Subject: [mg16041] [mg16036] Early out with Map? 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]