Function Composition
Function composition is a powerful technique that lets you combine multiple functions or predicates to build more complex operations from simpler ones. In C++, you can achieve function composition and chaining by using lambdas and standard algorithms. This approach is especially useful when you want to apply a series of transformations or filters to collections, such as vectors, in a concise and expressive way. By composing functions, you can create pipelines where the output of one function becomes the input of the next, making your code modular and easier to maintain.
main.cpp
1234567891011121314151617181920212223242526272829#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> numbers{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // First lambda: filter even numbers auto is_even = [](int x) { return x % 2 == 0; }; // Second lambda: square a number auto square = [](int x) { return x * x; }; // Compose: filter then transform std::vector<int> evens; std::copy_if(numbers.begin(), numbers.end(), std::back_inserter(evens), is_even); std::vector<int> squared_evens; std::transform(evens.begin(), evens.end(), std::back_inserter(squared_evens), square); std::cout << "Squared even numbers: "; for (int n : squared_evens) { std::cout << n << " "; } std::cout << std::endl; return 0; }
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Can you show an example of function composition in C++?
How do I use lambdas for chaining operations on a vector?
What are some best practices for composing functions in C++?
Mahtavaa!
Completion arvosana parantunut arvoon 4.55
Function Composition
Pyyhkäise näyttääksesi valikon
Function composition is a powerful technique that lets you combine multiple functions or predicates to build more complex operations from simpler ones. In C++, you can achieve function composition and chaining by using lambdas and standard algorithms. This approach is especially useful when you want to apply a series of transformations or filters to collections, such as vectors, in a concise and expressive way. By composing functions, you can create pipelines where the output of one function becomes the input of the next, making your code modular and easier to maintain.
main.cpp
1234567891011121314151617181920212223242526272829#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> numbers{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // First lambda: filter even numbers auto is_even = [](int x) { return x % 2 == 0; }; // Second lambda: square a number auto square = [](int x) { return x * x; }; // Compose: filter then transform std::vector<int> evens; std::copy_if(numbers.begin(), numbers.end(), std::back_inserter(evens), is_even); std::vector<int> squared_evens; std::transform(evens.begin(), evens.end(), std::back_inserter(squared_evens), square); std::cout << "Squared even numbers: "; for (int n : squared_evens) { std::cout << n << " "; } std::cout << std::endl; return 0; }
Kiitos palautteestasi!