}
};
-
/**
* Filter - For filtering values from a source sequence by a predicate.
*
* | filter([](const string& str) -> bool {
* return !str.empty();
* });
+ *
+ * Note that if no predicate is provided, the values are casted to bool and
+ * filtered based on that. So if pointers is a vector of pointers,
+ *
+ * auto nonNull = from(pointers) | filter();
+ *
+ * will give a vector of all the pointers != nullptr.
*/
template<class Predicate>
class Filter : public Operator<Filter<Predicate>> {
return Map(Field(field));
}
-template<class Predicate,
- class Filter = detail::Filter<Predicate>>
+template <class Predicate = Identity,
+ class Filter = detail::Filter<Predicate>>
Filter filter(Predicate pred = Predicate()) {
return Filter(std::move(pred));
}
EXPECT_EQ(expected, actual);
}
+TEST(Gen, FilterDefault) {
+ {
+ // Default filter should remove 0s
+ const auto expected = vector<int>{1, 1, 2, 3};
+ auto actual =
+ from({0, 1, 1, 0, 2, 3, 0})
+ | filter()
+ | as<vector>();
+ EXPECT_EQ(expected, actual);
+ }
+ {
+ // Default filter should remove nullptrs
+ int a = 5;
+ int b = 3;
+ int c = 0;
+ const auto expected = vector<int*>{&a, &b, &c};
+ auto actual =
+ from({(int*)nullptr, &a, &b, &c, (int*)nullptr})
+ | filter()
+ | as<vector>();
+ EXPECT_EQ(expected, actual);
+ }
+ {
+ // Default filter on Optionals should remove folly::null
+ const auto expected =
+ vector<Optional<int>>{Optional<int>(5), Optional<int>(0)};
+ const auto actual =
+ from({Optional<int>(5), Optional<int>(), Optional<int>(0)})
+ | filter()
+ | as<vector>();
+ EXPECT_EQ(expected, actual);
+ }
+}
+
TEST(Gen, Contains) {
{
auto gen =