Back

forEach

Overview

The forEach function iterates over each element in a Serie to perform an operation without changing the Serie itself. It's useful for side effects like printing or accumulating values.

Function Signatures


          // Single serie version
          template <typename F, typename T>
          auto forEach(F&& callback, const Serie<T>& serie);
          
          // Multi-series version
          template <typename F, typename T, typename... Args>
          auto forEach(F&& callback, const Serie<T>& first, const Serie<T>& second, const Args&... args);
          
          // Member function version
          template <typename F>
          void Serie<T>::forEach(F&& callback) const;
          
          // Bound version for pipeline operations
          template <typename F> auto bind_forEach(F&& callback);
        

Parameters

Parameter Type Description
callback F&& (callable) Function to apply to each element. For single Serie version, should accept (T value, size_t index). For multi-Serie version, should accept (T first_value, U second_value, ... args_values, size_t index).
serie const Serie<T>& The Serie to iterate over.
first, second, args... const Serie<T>&, const Serie<U>&, ... Multiple Series to iterate over in parallel.

Return Value

The forEach function does not return a value (void). It is used for its side effects.

Example Usage

Single Serie Example

// Create a Serie
df::Serie<int> numbers{1, 2, 3, 4, 5};

// Using Serie member function
numbers.forEach([](int value, size_t index) {
    std::cout << "Element " << index << ": " << value << std::endl;
});

// Using free function
df::forEach([](int value, size_t index) {
    std::cout << "Element " << index << ": " << value << std::endl;
}, numbers);

// Calculate sum (using a side effect)
int sum = 0;
numbers.forEach([&sum](int value, size_t) {
    sum += value;
});
std::cout << "Sum: " << sum << std::endl;  // Output: Sum: 15
Multiple Series Example

// Create multiple Series
df::Serie<std::string> names{"Alice", "Bob", "Charlie"};
df::Serie<int> ages{25, 32, 28};
df::Serie<double> heights{165.5, 180.2, 175.0};

// Process multiple Series together
df::forEach([](const std::string& name, int age, double height, size_t index) {
    std::cout << "Person " << index << ": " 
              << name << ", age " << age 
              << ", height " << height << " cm" << std::endl;
}, names, ages, heights);

// Output:
// Person 0: Alice, age 25, height 165.5 cm
// Person 1: Bob, age 32, height 180.2 cm
// Person 2: Charlie, age 28, height 175 cm
Pipeline Example

// Create a Serie
df::Serie<double> values{1.1, 2.2, 3.3, 4.4, 5.5};

// Use bind_forEach in a pipeline
values | df::bind_forEach([](double value, size_t index) {
    std::cout << "Value at " << index << ": " << value << std::endl;
});

// Multiple operations in a pipeline
df::Serie<int> numbers{1, 2, 3, 4, 5};
int sum = 0;

numbers
    | df::bind_map([](int x, size_t) { return x * 2; })  // Double each value
    | df::bind_forEach([&sum](int value, size_t) {       // Sum the doubled values
        sum += value;
    });
    
std::cout << "Sum of doubled values: " << sum << std::endl;  // Output: 30

Implementation Notes

  • The forEach function does not modify the input Serie(s).
  • For multiple Series, all Series must have the same size.
  • The callback function is called once for each element, in order from first to last.
  • The index parameter is optional in the callback function.

Related Functions