Last active
March 27, 2024 16:41
-
-
Save farhangamary/4b7a68963badb77a736abe1edcad92d0 to your computer and use it in GitHub Desktop.
A sample of meta and a sample of functional programming in c++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <vector> | |
| #include <ranges> | |
| #include <algorithm> | |
| using namespace std; | |
| int mod_by_six(int val); | |
| int main() | |
| { | |
| vector<vector<int>> mtx = { | |
| {112, 42, 83, 119}, | |
| {56, 125, 56, 49}, | |
| {15, 78, 101, 43}, | |
| {62, 98, 114, 108}}; | |
| function<vector<int>(vector<int>)> apply_mod = [](vector<int> vec) | |
| { | |
| transform(vec.begin(), vec.end(), vec.begin(), mod_by_six); | |
| return vec; | |
| }; | |
| auto rs = mtx | ranges::views::transform(apply_mod); | |
| for (auto v : rs) | |
| { | |
| for (auto nv : v) | |
| cout << nv << " "; | |
| cout << endl; | |
| } | |
| return 0; | |
| } | |
| int mod_by_six(int val) | |
| { | |
| if (val % 6 == 0) | |
| return 6; | |
| else if (val % 2 == 0) | |
| return 2; | |
| else if (val % 3 == 0) | |
| return 3; | |
| else | |
| return 0; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| template <long int N> | |
| struct MetaFactorial | |
| { | |
| enum | |
| { | |
| value = N * MetaFactorial<N - 1>::value | |
| }; | |
| }; | |
| template <> | |
| struct MetaFactorial<0> | |
| { | |
| enum | |
| { | |
| value = 1 | |
| }; | |
| }; | |
| int main() | |
| { | |
| std::cout << "Welcome to metaprogramming paradigm!" << std::endl | |
| << "Here everything is already calculated! and is ready for runtime." << std::endl | |
| << "It is the power of C++ templates, preprocessor macros, etc which" | |
| << " makes the C++ able to have metaprogramming paradigm." << std::endl | |
| << std::endl; | |
| std::cout << "The Factorial(10): " << MetaFactorial<10>::value << std::endl; | |
| std::cout << "The Factorial(13): " << MetaFactorial<13>::value << std::endl; | |
| } |
Author
farhangamary
commented
Mar 27, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment