www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Regarding partition_copy

reply "bearophile" <bearophileHUGS lycos.com> writes:
Splitting a range in two according to a predicate is a common
enough need. How do you translate this C++/STL idiom to D/Phobos?


vector<int> source_data;
...
vector<int> good_stuff, bad_stuff;
partition_copy(begin(source_data), end(source_data),
                 inserter(good_stuff, end(good_stuff)),
                 inserter(bad_stuff, end(bad_stuff)),
                 is_good);


If this is not handy to implement with the current Phobos, is it
worth adding a similar function to Phobos?

Thank you,
bye,
bearophile
May 05 2013
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 5/5/13 7:22 AM, bearophile wrote:
 Splitting a range in two according to a predicate is a common
 enough need. How do you translate this C++/STL idiom to D/Phobos?


 vector<int> source_data;
 ...
 vector<int> good_stuff, bad_stuff;
 partition_copy(begin(source_data), end(source_data),
 inserter(good_stuff, end(good_stuff)),
 inserter(bad_stuff, end(bad_stuff)),
 is_good);


 If this is not handy to implement with the current Phobos, is it
 worth adding a similar function to Phobos?

 Thank you,
 bye,
 bearophile
Yah, I've been thinking of adding a new category of functions that generate multiple ranges and conversely merge multiple ranges into one. A possibility here is to define segregate!pred(range) that takes a predicate and a range and returns a tuple of two ranges (for true and false values of the predicate). The two ranges can be consumed at different rates etc. That won't work with input ranges though. Andrei
May 05 2013