Back
filter
Overview
The filter function creates a new Serie containing only elements that satisfy a predicate function. It's useful for selecting a subset of data that meets specific criteria without modifying the original Serie.
Function Signatures
// Free function version with index
template <typename T>
Serie<T> filter(std::function<bool(const T&, size_t)> predicate,
const Serie<T>& serie);
// Free function version without index
template <typename T>
Serie<T> filter(std::function<bool(const T&)> predicate,
const Serie<T>& serie);
// Member function version
template <typename F>
Serie<T> Serie<T>::filter(F&& predicate) const;
// Bound version for pipeline operations with index
template <typename T>
auto bind_filter(std::function<bool(const T&, size_t)> predicate);
// Bound version for pipeline operations without index
template <typename T>
auto bind_filter(std::function<bool(const T&)> predicate);
Parameters
| Parameter | Type | Description |
|---|---|---|
| predicate | Function | Function that takes an element (and optionally its index) and returns a boolean value. If the predicate returns true, the element is included in the result; otherwise, it's excluded. |
| serie | const Serie<T>& | The Serie to filter. |
Return Value
A new Serie containing only the elements for which the predicate returns true. The returned Serie has the same element type as the input Serie but may have fewer elements.
Example Usage
Basic Filtering Examples
// Create a Serie of numbers
df::Serie<int> numbers{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// Filter even numbers using free function
auto even_numbers = df::filter([](int value) {
return value % 2 == 0;
}, numbers);
// even_numbers = {2, 4, 6, 8, 10}
// Filter odd numbers using member function
auto odd_numbers = numbers.filter([](int value) {
return value % 2 != 0;
});
// odd_numbers = {1, 3, 5, 7, 9}
// Filter numbers greater than 5
auto greater_than_five = df::filter([](int value) {
return value > 5;
}, numbers);
// greater_than_five = {6, 7, 8, 9, 10}
// Filter using index (keep elements at even indices)
auto even_indices = df::filter([](int, size_t idx) {
return idx % 2 == 0;
}, numbers);
// even_indices = {1, 3, 5, 7, 9} (elements at indices 0, 2, 4, 6, 8)
Complex Filtering
// Create a Serie of different types
df::Serie<double> temperatures{-5.2, 10.8, 15.4, -2.3, 22.1, 8.7, -8.9, 30.5};
// Filter positive temperatures
auto positive_temps = temperatures.filter([](double temp) {
return temp > 0;
});
// positive_temps = {10.8, 15.4, 22.1, 8.7, 30.5}
// Filter temperatures in a specific range
auto comfortable_temps = temperatures.filter([](double temp) {
return temp >= 18.0 && temp <= 25.0;
});
// comfortable_temps = {22.1}
// Custom object filtering
struct Person {
std::string name;
int age;
std::string city;
};
df::Serie<Person> people{
{"Alice", 28, "New York"},
{"Bob", 35, "Chicago"},
{"Charlie", 22, "New York"},
{"Diana", 41, "Boston"},
{"Eve", 19, "Chicago"}
};
// Filter people from New York
auto new_yorkers = people.filter([](const Person& p) {
return p.city == "New York";
});
// new_yorkers = {{"Alice", 28, "New York"}, {"Charlie", 22, "New York"}}
// Filter people older than 30
auto over_thirty = people.filter([](const Person& p) {
return p.age > 30;
});
// over_thirty = {{"Bob", 35, "Chicago"}, {"Diana", 41, "Boston"}}
Pipeline Examples
// Create a Serie of numbers
df::Serie<int> numbers{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// Use bind_filter in a pipeline
auto even_squared = numbers
| df::bind_filter<int>([](int value) { return value % 2 == 0; }) // Keep only even numbers
| df::bind_map([](int value, size_t) { return value * value; }); // Square the results
// even_squared = {4, 16, 36, 64, 100}
// Multiple filters in a pipeline
auto result = numbers
| df::bind_filter<int>([](int value) { return value > 3; }) // Keep numbers > 3
| df::bind_filter<int>([](int value) { return value < 9; }) // Keep numbers < 9
| df::bind_map([](int value, size_t) { return value * 10; }); // Multiply by 10
// result = {40, 50, 60, 70, 80}
// Complex pipeline with filter and other operations
df::Serie<double> values{-2.5, 3.7, -1.2, 4.8, -0.9, 6.2, -3.3};
auto processed = values
| df::bind_filter<double>([](double value) { return value > 0; }) // Keep positive values
| df::bind_map([](double value, size_t) { return std::sqrt(value); }) // Take square root
| df::bind_filter<double>([](double value) { return value > 2.0; }); // Keep values > 2.0
// processed = {2.19, 2.49}
Implementation Notes
- The filter function creates a new Serie and doesn't modify the original Serie.
- If no elements satisfy the predicate, an empty Serie is returned.
- The order of elements in the result is preserved from the original Serie.
- The filter function evaluates the predicate once for each element in the Serie.
- The index parameter in the predicate function is optional.
- In pipeline operations with multiple filters, the filters are applied sequentially from left to right.