Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Function Composition | Working with Predicates and Algorithms
C++ Functional Utilities

bookFunction 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

main.cpp

copy
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; }
question mark

What does function composition enable you to do when working with lambdas in C++?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 4

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

bookFunction Composition

Scorri per mostrare il menu

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

main.cpp

copy
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; }
question mark

What does function composition enable you to do when working with lambdas in C++?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 4
some-alt