MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Early out with Map?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg16053] Re: Early out with Map?
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Mon, 22 Feb 1999 01:44:32 -0500
  • References: <7alrat$nvj@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Oscar Stiffelman wrote in message <7alrat$nvj at smc.vnet.net>...
>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]
>


Oscar:

MemberQ[{1,1,0,1,1}, _?(#==0&)]

    True

MemberQ[{1,1,1,1,1}, _?(#==0&)]

    False

We can check that the search is stopped immediately a 0 is found:

MemberQ[{1,1,0,1,1}, _?(Print[z];#==0&)]

    z
    z
    z
    True

Allan

---------------------
Allan Hayes
Mathematica Training and Consulting
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565





  • Prev by Date: Re: SYMBOLIC Finite Differences
  • Next by Date: Re: implementing a stack
  • Previous by thread: RE: Early out with Map?
  • Next by thread: Re: Early out with Map?