Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // compile with -std=c++1y
- #include <iterator>
- #include <functional>
- #include <vector>
- #include <algorithm>
- namespace LoDash {
- using std::begin;
- using std::end;
- using std::vector;
- using std::function;
- struct LoDash {
- template<typename T>
- auto _pure_typeof(T x) -> typename std::remove_const<typename std::remove_reference<decltype(x)>::type>::type { };
- template<typename L>
- void each(const L& list, function<void(decltype(*begin(list))&)> iterator) {
- for (auto& i: list) iterator(i);
- }
- template<typename L>
- size_t count(const L& list) {
- return (size_t) std::distance(begin(list), end(list));
- }
- template<typename V, typename L>
- std::vector<V> map(const L& list, function<V(decltype(*begin(list)))> iterator) {
- std::vector<V> result;
- result.resize(count(list));
- std::transform(begin(list), end(list), result.begin(), iterator);
- return result;
- }
- template<typename L>
- auto map(const L& list, function<decltype(*begin(list))(decltype(*begin(list)))> iterator) -> std::vector<decltype(_pure_typeof(*begin(list)))> {
- return map<decltype(_pure_typeof(*begin(list))), L>(list, iterator);
- }
- };
- }
- auto _ = LoDash::LoDash();
- #include <set>
- #include <iostream>
- #include <map>
- using namespace std;
- #define lambda [&]
- int main(int argc, char const *argv[]) {
- int a[] = {1, 2};
- _.each(a, lambda (auto x) { std::cout << x << "\n"; });
- std::set<int> s = {3, 4};
- _.each(s, lambda (auto x) { std::cout << x << "\n"; });
- std::vector<int> v = {5, 6};
- _.each(v, lambda (auto x) { std::cout << x << "\n"; });
- auto m1 = _.map(v, lambda (auto x) { return -x; });
- _.each(m1, lambda (auto x) { std::cout << x << "\n";});
- auto m2 = _.map<char>(v, lambda (int x) { return 'a' + x; });
- _.each(m2, lambda (auto x) { std::cout << x << "\n";});
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement