Created
June 13, 2014 18:16
-
-
Save oakfang/98fcd89513b3305944f1 to your computer and use it in GitHub Desktop.
python macros
This file contains 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 <functional> | |
#define var auto | |
#define in : | |
#define lambda(...) [](__VA_ARGS__) | |
#define print(X) std::cout<<X<<std::endl | |
#define def(TYPE, FUNC, ...) auto FUNC(__VA_ARGS__) -> TYPE | |
/* | |
SUBLIME BUILD SYSTEM (C++11): | |
{ | |
"shell_cmd": "g++ -Wall -std=c++11 -o ${file_path}/${file_base_name}.exe $file" | |
} | |
*/ | |
template<typename T, class Function> | |
std::vector<T> filter (Function predicate, std::vector<T> const &orig) { | |
std::vector<T> v; | |
for (var x in orig) { | |
if (predicate(x)) { | |
v.push_back(x); | |
} | |
} | |
return v; | |
} | |
template<typename T, class Function> | |
std::vector<T> vmap (Function mapper, std::vector<T> const &orig) { | |
std::vector<T> v(orig.size()); | |
size_t i = 0; | |
for (var x in orig) { | |
v[i] = mapper(x); | |
i++; | |
} | |
return v; | |
} | |
int main(int argc, char **argv) { | |
print("Testin gout macros:"); | |
print("var test:"); | |
var n = 15; | |
n++; | |
print(n); | |
std::vector<int> v; | |
v.push_back(1); | |
v.push_back(2); | |
v.push_back(3); | |
print("for each test:"); | |
for (var num in v) { | |
std::cout << num << std::endl; | |
} | |
print("lambda test:"); | |
var myFunc = lambda(int n){ | |
return n * n; | |
}; | |
print(myFunc(3)); | |
print("filter test:"); | |
for (var num in filter(lambda(int x){return x % 2 == 0;}, v)) { | |
print(num); | |
} | |
print("vmap test:"); | |
for (var num in vmap(lambda(int x){return x * x;}, v)) { | |
print(num); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment