views:

73

answers:

6

I have a container class (containing a multi-index container) for which I have a public "foreach" member-function, so users can pass a functor to apply on all elements.
While implementing, I had a case where the functor should only be applied to some elements of a range in the container, so I overloaded the foreach, to pass some valid range.
Now, in some cases, it was worthwhile to stop on a certain condition, so practically, I let the foreach stop based on the return-value of the function.

I'm pleased with how the system works, but I have one concern:
How should a "foreach" on a range, with stop conditions be called?
Anyone knows a generic, clear and concise name?

A: 

a generic name? ConditionalIterator? RangeIterator? Or since it's a function, perhaps IterateRange?

David Hedlund
A: 

How about find_if(...), as in stl ?

Viktor Sehr
+2  A: 

Maybe until or something?

Andreas Brinck
+6  A: 

Based on your description, i'd go for apply_until().

Georg Fritzsche
I like the apply_... name because it shows that it is an interface to apply functions. Strangely enough, I put it in the the title, but it hasn't crossed my mind to actually use it as a function name. I guess, foreach was stuck too much in my head.
stefaanv
+1  A: 

I'd probably call it either foreach_until or foreach_while (attempting to follow a convention based on the _if functions like replace_if and remove_if, except that the condition here isn't "if" each one is true, it's "until" the end condition holds). Or just foreach. std::for_each ignores the return value of the functor, but that doesn't necessarily mean that your function called foreach has to. I think it's a pretty common idiom for a callback function to have a return value allowing early exit from the controlling loop.

If the callback returns either a false value (to continue) or a true value (to halt), then you could call the function find_if. That's basically what it is, and it could be implemented using std::find_if, even if the caller isn't interested in which iterator provoked the end condition.

Steve Jessop
I am intrigued by the find_if, because it is answered twice. I thought that "find..."-functions are about returning members and that "..._if" functions used "predicates" as in "pure functions that return a boolean based on some condition". Am I being too strict?
stefaanv
Predicates return a value "testable as true", not necessarily a boolean. They aren't required to be pure, but they must not call non-const functions on their parameter: I neglected to mention that as a condition. All in 25/7. You're right that `find` functions are about returning the iterator that triggered the "stop", but you can always ignore a return value. Really I just mean that if `find_if` is "close enough" to what you want, then use it even if conceptually you aren't finding anything. If it's not "close enough", don't use that name :-)
Steve Jessop
+1  A: 

If you can provide a subset function that returns a view of the elements of interest, maybe you don't need apply_until(). Just myContainer.subset(3, 17).foreach(myFunctor) I think that's a better separation of concerns.

Carl Manaster
Wow, a sort of different approach. It sounds nice. Practically for me, it would mean having another class (the subset) with access to the internal container, though.
stefaanv
On second thought, it would only need 2 iterators, so I should keep this in mind
stefaanv