# functional

***

**1. Vector Transformation**

```cpp
#include <vector>

int main() {
  std::vector<int> nums = {1, 2, 3, 4, 5};
  std::transform(nums.begin(), nums.end(), nums.begin(), [](int n) { return n * 2; });
  for (const auto& n : nums) {
    std::cout << n << " "; // Output: 2 4 6 8 10
  }
}
```

**2. Filtering a Collection**

```cpp
#include <vector>
#include <algorithm>

int main() {
  std::vector<int> nums = {1, 2, 3, 4, 5};
  std::vector<int> evens;
  std::copy_if(nums.begin(), nums.end(), std::back_inserter(evens), [](int n) { return n % 2 == 0; });
  for (const auto& n : evens) {
    std::cout << n << " "; // Output: 2 4
  }
}
```

**3. Summing a Collection**

```cpp
#include <vector>
#include <numeric>

int main() {
  std::vector<int> nums = {1, 2, 3, 4, 5};
  int sum = std::accumulate(nums.begin(), nums.end(), 0);
  std::cout << "Sum: " << sum << std::endl; // Output: Sum: 15
}
```

**4. Finding the Maximum Element**

```cpp
#include <vector>
#include <algorithm>

int main() {
  std::vector<int> nums = {1, 2, 3, 4, 5};
  int max = *std::max_element(nums.begin(), nums.end());
  std::cout << "Maximum: " << max << std::endl; // Output: Maximum: 5
}
```

**5. Finding the First Odd Element**

```cpp
#include <vector>
#include <algorithm>

int main() {
  std::vector<int> nums = {1, 2, 4, 6, 8, 10, 3};
  auto it = std::find_if(nums.begin(), nums.end(), [](int n) { return n % 2 == 1; });
  if (it != nums.end()) {
    std::cout << "First odd element: " << *it << std::endl; // Output: First odd element: 3
  }
}
```

**6. Sorting a Collection**

```cpp
#include <vector>
#include <algorithm>

int main() {
  std::vector<int> nums = {3, 1, 4, 2, 5};
  std::sort(nums.begin(), nums.end());
  for (const auto& n : nums) {
    std::cout << n << " "; // Output: 1 2 3 4 5
  }
}
```

**7. Reversing a Collection**

```cpp
#include <vector>
#include <algorithm>

int main() {
  std::vector<int> nums = {1, 2, 3, 4, 5};
  std::reverse(nums.begin(), nums.end());
  for (const auto& n : nums) {
    std::cout << n << " "; // Output: 5 4 3 2 1
  }
}
```

**8. Generating a Sequence**

```cpp
#include <iostream>
#include <iterator>

int main() {
  std::generate_n(std::ostream_iterator<int>(std::cout, " "), 5, []() { return std::rand() % 10; });
  // Output: 7 6 2 5 9
}
```

**9. Generating a Fibonacci Sequence**

```cpp
#include <iostream>
#include <iterator>

int main() {
  std::generate_n(std::ostream_iterator<int>(std::cout, " "), 10, [](int n) {
    if (n == 0) return 0;
    if (n == 1) return 1;
    return std::prev(std::prev(std::ostream_iterator<int>(std::cout, " ")), 2)->__value + std::prev(std::ostream_iterator<int>(std::cout, " "), 1)->__value;
  });
  // Output: 0 1 1 2 3 5 8 13 21 34
}
```

**10. Finding the GCD (Greatest Common Divisor)**

```cpp
#include <algorithm>
#include <numeric>

int gcd(int a, int b) {
  return a == 0 ? b : gcd(b % a, a);
}

int main() {
  std::cout << gcd(12, 18) << std::endl; // Output: 6
}
```

**11. Finding the LCM (Least Common Multiple)**

```cpp
#include <algorithm>

int lcm(int a, int b) {
  return (a * b) / gcd(a, b);
}

int main() {
  std::cout << lcm(12, 18) << std::endl; // Output: 36
}
```

**12. Creating a Lambda Function**

```cpp
int main() {
  auto square = [](int n) { return n * n; };
  std::cout << square(5) << std::endl; // Output: 25
}
```

**13. Using Lambda Functions with Algorithms**

```cpp
#include <vector>
#include <algorithm>

int main() {
  std::vector<int> nums = {1, 2, 3, 4, 5};
  std::vector<int> squares(nums.size());
  std::transform(nums.begin(), nums.end(), squares.begin(), [](int n) { return n * n; });
  for (const auto& n : squares) {
    std::cout << n << " "; // Output: 1 4 9 16 25
  }
}
```

**14. Creating a Functor**

```cpp
struct Square {
  int operator()(int n) { return n * n; }
};

int main() {
  Square square;
  std::cout << square(5) << std::endl; // Output: 25
}
```

**15. Partially Applying Functions**

```cpp
#include <vector>
#include <algorithm>
#include <functional>

int main() {
  std::vector<int> nums = {1, 2, 3, 4, 5};
  int target = 3;
  std::vector<int> results;
  std::copy_if(nums.begin(), nums.end(), std::back_inserter(results), std::bind(std::equal_to<int>(), std::placeholders::_1, target));
  for (const auto& n : results) {
    std::cout << n << " "; // Output: 3
  }
}
```

**16. Creating a Curried Function**

```cpp
#include <functional>

auto curry(auto f) {
  return std::bind(f, std::placeholders::_1, std::placeholders::_2);
}

int add(int x, int y) { return x + y; }

int main() {
  auto add_5 = curry(add)(5);
  std::cout << add_5(10) << std::endl; // Output: 15
}
```

**17. Using Function Pointers**

```cpp
#include <functional>
#include <vector>
#include <algorithm>

int main() {
  std::vector<int> nums = {1, 2, 3, 4, 5};
  std::sort(nums.begin(), nums.end(), std::less<int>());
  for (const auto& n : nums) {
    std::cout << n << " "; // Output: 1 2 3 4 5
  }
}
```

**18. Using Function Objects**

```cpp
#include <vector>
#include <algorithm>
#include <functional>

struct Compare {
  bool operator()(int a, int b) { return a < b; }
};

int main() {
  std::vector<int> nums = {1, 2, 3, 4, 5};
  std::sort(nums.begin(), nums.end(), Compare());
  for (const auto& n : nums) {
    std::cout << n << " "; // Output: 1 2 3 4 5
  }
}
```

**19. Using Boost.Lambda**

```cpp
#include <boost/lambda/lambda.hpp>
#include <vector>

int main() {
  std::vector<int> nums = {1, 2, 3, 4, 5};
  std::vector<int> squares = boost::lambda::transform(nums, boost::lambda::_1 * boost::lambda::_1);
  for (const auto& n : squares) {
    std::cout << n << " "; // Output: 1 4 9 16 25
  }
}
```

**20. Using Expression Templates**

```cpp
#include <iostream>
#include <functional>

template <typename F>
auto foo(F f) {
  return f(5);
}

int main() {
  auto lambda = [](int x) { return x * x; };
  std::cout << foo(lambda) << std::endl; // Output: 25
}
```

**21. Using C++20 Concepts**

```cpp
#include <iostream>
#include <concepts>

template <typename T>
concept Integer = std::requires {requires(T t) { t + 1; };};

int main() {
  using namespace std::literals;
  Integer auto x = 10;
  std::cout << x + 5 << std::endl; // Output: 15
}
```

**22. Using C++20 Range-based For Loops**

```cpp
#include <iostream>
#include <vector>
#include <ranges>

int main() {
  std::vector<int> nums = {1, 2, 3, 4, 5};
  for (int n : nums) {
    std::cout << n << " "; // Output: 1 2 3 4 5
  }
}
```

**23. Using C++20 Structured Bindings**

```cpp
#include <iostream>
#include <tuple>

int main() {
  std::tuple<int, std::string> t = {5, "hello"};
  auto [a, b] = t;
  std::cout << a << " " << b << std::endl; // Output: 5 hello
}
```

**24. Using C++20 Constexpr Functions**

```cpp
constexpr int fact(int n) { return n <= 1 ? 1 : n * fact(n - 1); }

int main() {
  std::cout << fact(5) << std::endl; // Output: 120
}
```

**25. Using C++20 Lambda Captures**

```cpp
#include <iostream>
#include <vector>

int main() {
  int x = 5;
  auto lambda = [x](int y) { return x + y; };
  std::cout << lambda(10) << std::endl; // Output: 15
}
```

**26. Using C++20 Default Function Templates**

```cpp
#include <iostream>
#include <vector>

template <typename T = int>
std::vector<T> createVector(size_t size) {
  return std::vector<T>(size);
}

int main() {
  auto v = createVector<double>(5);
  for (const auto& x : v) {
    std::cout << x << " "; // Output: 0 0 0 0 0
  }
}
```

**27. Using C++20 Template Alias**

```cpp
#include <iostream>
#include <vector>

template <typename T>
using Vector = std::vector<T>;

int main() {
  Vector<int> v = {1, 2, 3, 4, 5};
  for (const auto& x : v) {
    std::cout << x << " "; // Output: 1 2 3 4 5
  }
}
```

**28. Using C++20 Concepts with Templates**

```cpp
#include <iostream>
#include <vector>
#include <concepts>

template <typename T>
concept Integer = requires(T x) { requires { x + 1; }; };

template <typename T>
requires Integer<T>
std::vector<T> createVector(size_t size) {
  return std::vector<T>(size);
}

int main() {
  auto v = createVector<int>(5);
  for (const auto& x : v) {
    std::cout << x << " "; // Output: 0 0 0 0 0
  }
}
```

**29. Using C++20 Fold Expressions**

```cpp
#include <iostream>
#include <vector>

template <typename T>
auto sum(const std::vector<T>& v) {
  return std::reduce(v.begin(), v.end(), T{});
}

int main() {
  std::vector<int> v = {1, 2, 3, 4, 5};
  std::cout << sum(v) << std::endl; // Output: 15
}
```

**30. Using C++20 Coroutines**

```cpp
#include <iostream>
#include <coroutine>

struct Generator {
  struct promise_type {
    using coro_return_type = int;
    int current_value;
    auto get_return_object() { return Generator{coroutine_handle<promise_type>::from_promise(*this)}; }
    auto initial_suspend() { return std::suspend_always{}; }
    auto final_suspend() { return std::suspend_always{}; }
    void return_value(int value) { current_value = value; }
    void unhandled_exception() {}
  };

  bool done = false;
  coroutine_handle<promise_type> coro;

  Generator(coroutine_handle<promise_type> coro_) : coro(coro_) {}

  int operator()() {
    if (done) {
      return promise_type::current_value;
    }
    coro.resume();
    done = true;
    return promise_type::current_value;
  }
};

Generator counter() {
  for (int i = 0;; i++) {
    co_yield i;
  }
}

int main() {
  auto c = counter();
  std::cout << c() << " " << c() << " " << c() << std::endl; // Output: 0 1 2
}
```

**31. Using C++20 Modules**

```cpp
// counter.cpp
export module counter;

export Generator getCounter();

struct Generator {
  struct promise_type {
    using coro_return_type = int;
    int current_value;
    auto get_return_object() { return Generator{coroutine_handle<promise_type>::from_promise(*this)}; }
    auto initial_suspend() { return std::suspend_always{}; }
    auto final_suspend() { return std::suspend_always{}; }
    void return_value(int value) { current_value = value; }
    void unhandled_exception() {}
  };

  bool done = false;
  coroutine_handle<promise_type> coro;

  Generator(coroutine_handle<promise_type> coro_) : coro(coro_) {}

  int operator()() {
    if (done) {
      return promise_type::current_value;
    }
    coro.resume();
    done = true;
    return promise_type::current_value;
  }
};

Generator getCounter() {
  for (int i = 0;; i++) {
    co_yield i;
  }
}
```

```cpp
// main.cpp
import counter;

int main() {
  auto c = counter::getCounter();
  std::cout << c() << " " << c() << " " << c() << std::endl; // Output: 0 1 2
}
```

**32. Using C++20 Concepts with Modules**

```cpp
// counter.cpp
export module counter;

export requires Integer<T> Generator<T>;

template <typename T>
concept Integer = requires(T x) { requires { x + 1; }; };

template <typename T>
requires Integer<T>
struct Generator {
  struct promise_type {
    using coro_return_type = T;
    T current_value;
    auto get_return_object() { return Generator{coroutine_handle<promise_type>::from_promise(*this)}; }
    auto initial_suspend() { return std::suspend_always{}; }
    auto final_suspend() { return std::suspend_always{}; }
    void return_value(T value) { current_value = value; }
    void unhandled_exception() {}
  };

  bool done = false;
  coroutine_handle<promise_type> coro;

  Generator(coroutine_handle<promise_type> coro_) : coro(coro_) {}

  T operator()() {
    if (done) {
      return promise_type::current_value;
    }
    coro.resume();
    done = true;
    return promise_type::current_value;
  }
};

export Generator<int> getCounter();

Generator<int> getCounter() {
  for (int i = 0;; i++) {
    co_yield i;
  }
}
```

**33. Using C++20 Constexpr Modules**

```cpp
// counter.cpp
export module counter;

export constexpr Generator<int> getCounter();

template <typename T>
struct Generator {
  struct promise_type {
    using coro_return_type = T;
    T current_value;
    auto get_return_object() { return Generator{coroutine_handle<promise_type>::from_promise(*this)}; }
    auto initial_suspend() { return std::suspend_always{}; }
    auto final_suspend() { return std::suspend_always{}; }
    void return_value(T value) { current_value = value; }
    void unhandled_exception() {}
  };

  bool done = false;
  coroutine_handle<promise_type> coro;

  Generator(coroutine_handle<promise_type> coro_) : coro(coro_) {}

  T operator()() {
    if (done) {
      return promise_type::current_value;
    }
    coro.resume();
    done = true;
    return promise_type::current_value;
  }
};

constexpr Generator<int> getCounter() {
  for (int i = 0;; i++) {
    co_yield i;
  }
}
```

```cpp
// main.cpp
import counter;

int main() {
  auto c = counter::getCounter();
  std::cout << c() << " " << c() << " " << c() << std::endl; // Output: 0 1 2
}
```

\*\*34. Using C++2
