for_each_until like algorithm
Hi, I just wondered if there was already an algorithm (or way to compose one) in Boost that would allow me to iterate over a container of pointers to objects calling a function on each one and completing when one of them returned a particular value? For example, I have a master/child relationship and the master has a method called shouldCalculate(). Whether this returns true is partly dependent on what the children return for their shouldCalculate() method. So I'd like to say something like this: bool Master::shouldCalculate() { // Look at data at the master level to see if it's stale. If it is, return true. // See if any of the children should calculate bool staleChildren = boost::for_each_until(children_.begin(),children_.end(), boost::bind(&Child::shouldCalculate,_1),until_true_returned); return staleChildren; } Regards, Pete
On Wed, Feb 11, 2009 at 12:06 PM, Peter Barker
Hi,
I just wondered if there was already an algorithm (or way to compose one) in Boost that would allow me to iterate over a container of pointers to objects calling a function on each one and completing when one of them returned a particular value?
For example, I have a master/child relationship and the master has a method called shouldCalculate(). Whether this returns true is partly dependent on what the children return for their shouldCalculate() method. So I'd like to say something like this:
bool Master::shouldCalculate() { // Look at data at the master level to see if it's stale. If it is, return true.
// See if any of the children should calculate bool staleChildren = boost::for_each_until(children_.begin(),children_.end(),
boost::bind(&Child::shouldCalculate,_1),until_true_returned);
return staleChildren; }
Regards,
Pete
Sorry for the noise. Just figured std::find_if() should do it. Regards, Pete
On Wed, Feb 11, 2009 at 12:21 PM, Peter Barker
Sorry for the noise. Just figured std::find_if() should do it.
Hi Peter Although you've quickly figured out that the functionality you need is already there, I do agree that the use of find_if() to express until() semantics is not immediately obvious. I think it is proabably a little "Aha" moment for many of us. In a similar vein I often find myself writing this functionality bool exists( begin, end, value ) { return find(begin, end, value) != end; } and bool exists_if( begin, end, predicate) { return find_if(begin, end, predicate) != end; } So maybe there is a place for some very thin wrappers. Regards, Rob.
Robert Jones
On Wed, Feb 11, 2009 at 12:21 PM, Peter Barker <mailto:newbarker@gmail.comnewbarker@gmail.com> wrote:
Sorry for the noise. Just figured std::find_if() should do it.
Hi Peter
Although you've quickly figured out that the functionality you need is already there, I do agree that the use of find_if() to express until() semantics is not immediately obvious. I think it is proabably a little "Aha" moment for many of us. In a similar vein I often find myself writing this functionality
bool exists( begin, end, value ) { return find(begin, end, value) != end; }
and
bool exists_if( begin, end, predicate) { return find_if(begin, end, predicate) != end; }
So maybe there is a place for some very thin wrappers.
You might look in the sandbox, specifically in boost/algorithms/all.hpp. What you call "exists" is called "any" there - which follows the proposal for TR2. -- -- Marshall Marshall Clow Idio Software mailto:marshall@idio.com It is by caffeine alone I set my mind in motion. It is by the beans of Java that thoughts acquire speed, the hands acquire shaking, the shaking becomes a warning. It is by caffeine alone I set my mind in motion.
participants (3)
-
Marshall Clow
-
Peter Barker
-
Robert Jones