Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // compile with -std=c++1y
- #include <algorithm>
- #include <array>
- #include <ext/algorithm>
- #include <functional>
- #include <iterator>
- #include <vector>
- 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))&)> func) {
- for (auto& i: list) func(i);
- }
- template<typename L>
- size_t count(const L& list) {
- return (size_t) std::distance(begin(list), end(list));
- }
- template<typename L>
- size_t count(const L& list, function<bool(decltype(*begin(list)))> func) {
- return (size_t) std::count_if(begin(list), end(list), func);
- }
- template<typename L, typename F>
- auto map(const L& list, F func) -> vector<decltype(func(*begin(list)))> {
- typedef decltype(func(*begin(list))) FR;
- vector<FR> result;
- result.resize(count(list));
- std::transform(begin(list), end(list), result.begin(), func);
- return result;
- }
- template<typename L>
- auto select(const L& list, function<bool(decltype(*begin(list)))> func) -> vector<decltype(_pure_typeof(*begin(list)))> {
- vector<decltype(_pure_typeof(*begin(list)))> result;
- std::copy_if(begin(list), end(list), std::back_inserter(result), func);
- return result;
- }
- template<typename L>
- auto sample(const L& list, size_t n) -> vector<decltype(_pure_typeof(*begin(list)))> {
- vector<decltype(_pure_typeof(*begin(list)))> result;
- result.resize(n);
- // random_sample_n is a SGI extension
- __gnu_cxx::random_sample_n(begin(list), end(list), result.begin(), n);
- return result;
- }
- template<typename L>
- auto sample(const L& list) -> decltype(_pure_typeof(*begin(list))) {
- std::array<decltype(_pure_typeof(*begin(list))), 1> arr;
- __gnu_cxx::random_sample_n(begin(list), end(list), arr.begin(), 1);
- return arr[0];
- }
- template<typename L>
- auto sort(const L& list) -> vector<decltype(_pure_typeof(*begin(list)))> {
- vector<decltype(_pure_typeof(*begin(list)))> result;
- result.reserve(count(list));
- std::copy(begin(list), end(list), std::back_inserter(result));
- std::stable_sort(result.begin(), result.end());
- return result;
- }
- template<typename L>
- auto min(const L& list) -> decltype(_pure_typeof(*begin(list))) {
- decltype(_pure_typeof(*begin(list))) result = *begin(list);
- each(list, [&] (auto x) { if (x < result) result = x;});
- return result;
- }
- template<typename L>
- auto max(const L& list) -> decltype(_pure_typeof(*begin(list))) {
- decltype(_pure_typeof(*begin(list))) result = *begin(list);
- each(list, [&] (auto x) { if (x > result) result = x;});
- return result;
- }
- template<typename L, typename F>
- auto min_by(const L& list, F func) -> decltype(_pure_typeof(*begin(list))) {
- typedef decltype(func(*begin(list))) FR;
- if (count(list) == 0) return FR();
- decltype(_pure_typeof(*begin(list))) result = *begin(list);
- FR val = func(*begin(list));
- each(list, [&] (auto x) { if (x != result) { FR tmp = func(x); if (func(x) < val) {result = x; val = tmp; }}});
- return result;
- }
- template<typename L, typename F>
- auto max_by(const L& list, F func) -> decltype(_pure_typeof(*begin(list))) {
- typedef decltype(func(*begin(list))) FR;
- if (count(list) == 0) return FR();
- decltype(_pure_typeof(*begin(list))) result = *begin(list);
- FR val = func(*begin(list));
- each(list, [&] (auto x) { if (x != result) { FR tmp = func(x); if (func(x) > val) {result = x; val = tmp; }}});
- return result;
- }
- };
- }
- auto _ = LoDash::LoDash();
- #include <set>
- #include <iostream>
- #include <string>
- #define lambda [&]
- #define P(r) { std::cout << "[ "; _.each((r), lambda (auto x) { std::cout << x << " ";}); std::cout << "]\n"; }
- int main(int argc, char const *argv[]) {
- int a[] = {1, 2, 3, 4};
- std::set<int> s = {8, 7, 6, 5};
- std::vector<int> v = {9, 10, 11, 12};
- P(a); // 1 2 3 4
- P(s); // 5 6 7 8
- P(v); // 9 10 11 12
- P(_.map(s, lambda (auto x) { return -x; })); // -5 -6 -7 -8
- P(_.map(v, lambda (auto x) { return (char)('a' + x); })); // j k l m
- P(_.select(v, lambda (auto x) { return (x & 1); })); // 9 11
- P(_.sample(s, 3));
- P(_.sort(a)); // 1 2 3 4
- std::cout << _.sample(a) << "\n"; // 1 or 2 or 3 or 4
- std::cout << _.count(a, lambda (auto x) { return x > 1; }) << "\n"; // 3
- std::cout << _.min(a) << "\n"; // 1
- std::cout << _.min_by(a, lambda (int x) {return -x;}) << "\n"; // 4
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement