Re: Early out with Map?
- To: mathgroup at smc.vnet.net
- Subject: [mg16038] Re: Early out with Map?
- From: Mark Fisher <mefisher at bellsouth.net>
- Date: Sun, 21 Feb 1999 00:15:16 -0500
- References: <7alrat$nvj@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Something like this might work.
list1 = {1, 2, 3, 3, 3}
list2 = {1, 2, 3, 4, 5}
Catch[Scan[If[# > 3, Throw[True]]&, list1]; False] --> False
Catch[Scan[If[# > 3, Throw[True]]&, list2]; False] --> True
--Mark.
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]