Last active
September 15, 2018 06:10
-
-
Save MizukiSonoko/9f9810009f2e4543fca6013b7dda6a82 to your computer and use it in GitHub Desktop.
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 <functional> | |
#include <algorithm> | |
#include <vector> | |
#include <list> | |
#include <array> | |
#include <string> | |
#include <initializer_list> | |
#include <initializer_list> | |
#include <iostream> | |
#include <stdexcept> | |
#include <type_traits> | |
#include <vector> | |
using std::cout; | |
using std::endl; | |
template<class T> | |
struct List : public std::vector<T>{ | |
List(std::initializer_list<T> al): | |
std::vector<T>(al) | |
{} | |
List map(auto function){ | |
std::transform( this->begin(), this->end(), this->begin(), function); | |
return *this; | |
} | |
typename std::vector<T>::iterator begin(){ | |
// cout<<"begin " << *this->std::vector<T>::begin() << endl; | |
return this->std::vector<T>::begin(); | |
} | |
typename std::vector<T>::iterator end(){ | |
// cout<<"end " << *this->std::vector<T>::end() << endl; | |
return this->std::vector<T>::end(); | |
} | |
List& print(){ | |
cout<<"start == in print == \n"; | |
for(const auto& v: *this){ | |
cout << v << endl; | |
} | |
cout<<"end == in print ==\n"; | |
return *this; | |
} | |
}; | |
template<typename T> | |
auto l(std::initializer_list<T> iniList) -> List<T>{ | |
return List<T>(iniList); | |
} | |
int main(){ | |
for(const auto& v: l({ 1, 2, 3, 4, 5}) | |
.map([](auto v){ return v + 1;}) | |
.map([](auto v){ return v + 1;}) | |
){ | |
cout << v <<" "<< endl; | |
} | |
return 0; | |
} |
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 <algorithm> | |
#include <cmath> | |
#include <vector> | |
namespace my_type{ | |
struct Type{ | |
int value; | |
Type(int v):value(v){} | |
auto operator ()(const long long& last){ | |
std::vector<int> v(abs(value-last)); | |
std::generate(v.begin(), v.end(), [&number = value,value = value,last](){ | |
if(value - last < 0){ | |
return number++; | |
}else{ | |
return number--; | |
} | |
}); | |
return v; | |
} | |
}; | |
Type operator"" _to (unsigned long long n){ | |
return Type(n); | |
} | |
}; | |
int main(){ | |
using namespace my_type; | |
for(const auto& c: 10_to(20)){ | |
std::cout<<"num:"<< c << std::endl; | |
} | |
std::cout<<"====="<< std::endl; | |
for(const auto& c: 20_to(10)){ | |
std::cout<<"num:"<< c << std::endl; | |
} | |
return 0; | |
} |
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 <functional> | |
#include <algorithm> | |
#include <vector> | |
#include <list> | |
#include <array> | |
#include <string> | |
#include <initializer_list> | |
#include <iostream> | |
#include <stdexcept> | |
#include <type_traits> | |
#include <vector> | |
using std::cout; | |
using std::endl; | |
template<typename T> | |
using function = std::function<T>; | |
template<class T> | |
struct List : public std::vector<T>{ | |
List(long long len): | |
std::vector<T>(len) | |
{} | |
List(std::initializer_list<T> al): | |
std::vector<T>(al) | |
{} | |
List map(const function<T(T)>& f){ | |
std::transform( this->begin(), this->end(), this->begin(), f); | |
return *this; | |
} | |
List flatten(const function<T(T)>& f){ | |
std::transform( this->begin(), this->end(), this->begin(), f); | |
return *this; | |
} | |
List filter(const function<bool(T)>& f){ | |
for(auto& v: *this){ | |
if(!f(v)){ | |
this->erase(std::remove( this->begin(), this->end(), v), this->end()); | |
} | |
} | |
return *this; | |
} | |
void zipWithIndex(const function<void(T,int)>&& f){ | |
size_t position = 0; | |
for(auto&& v: *this){ | |
f( v, position); | |
position++; | |
} | |
} | |
T reduce_impl(const function<T(T,T)>& f,const T& v1, const T& v2, List<T>* list){ | |
if(list->empty()){ | |
return f( v1, v2); | |
}else{ | |
auto top = *list->begin(); | |
list->erase(this->begin()); | |
return reduce_impl( f, f( v1, v2), top, list); | |
} | |
} | |
T reduce(const function<T(T,T)>&& f){ | |
if(this->size() >= 2){ | |
auto v1 = this->at(0);auto v2 = this->at(1); | |
this->erase(this->begin());this->erase(this->begin()); | |
return reduce_impl( f, v1, v2, this); | |
}else{ | |
return this->at(0); | |
} | |
} | |
void foreach(const function<void(T)>& f){ | |
for(const auto& v: *this){ | |
f(v); | |
} | |
} | |
auto stringify(T&& t) -> decltype(std::to_string(std::forward<T>(t))) { | |
return std::to_string(std::forward<T>(t)); | |
} | |
std::string mkString_impl(const std::string& cur,const std::string& interval,typename std::vector<T>::iterator it){ | |
++it; | |
if(it == this->end()){ | |
return cur; | |
}else{ | |
return mkString_impl(cur+interval+stringify(std::move(*it)),interval, it); | |
} | |
} | |
std::string mkString(std::string interval){ | |
return mkString_impl( stringify(std::move(*this->begin())), interval, this->begin()); | |
} | |
}; | |
template<typename T> | |
auto L(std::initializer_list<T> iniList) -> List<T>{ | |
return List<T>(iniList); | |
} | |
template<typename T, typename... Args> | |
auto L(T const& head, Args... tail) { | |
return List<T>({head, tail...}); | |
} | |
template<typename T> | |
using l = List<T>; | |
struct Generator{ | |
int value; | |
Generator(int v):value(v){} | |
auto operator ()(const long long& last,const unsigned long long& step = 1){ | |
List<int> v(std::abs(value-last)/step); | |
std::generate(v.begin(), v.end(), [&number = value,value = value,last,step](){ | |
if(value - last < 0) { | |
return number += step; | |
}else{ | |
return number -= step; | |
} | |
}); | |
return v; | |
} | |
}; | |
Generator operator"" _to (unsigned long long n){ | |
return Generator(n); | |
} | |
int main(){ | |
l<int>{ 1, 2, 3, 4, 5} | |
.map([](auto v){ return v + 1;}) | |
.filter([](auto v)-> bool{ return v % 2;}) | |
.foreach([](auto v){ | |
cout << v <<" "<< endl; | |
}); | |
auto sum = l<int>{ 1, 2, 3, 4, 5} | |
.reduce([](auto a,auto b){ return a + b; }); | |
cout <<"sum is "<< sum <<endl; | |
auto str = l<int>{ 1, 2, 3, 4, 5} | |
.mkString(", "); | |
cout <<"mkString is "<< str << endl; | |
l<double>{ 1.0, 2.0, 3.0, 4.0, 5.0}.zipWithIndex([](auto v,int p){ | |
cout<<"array["<< p <<"]= "<< v <<endl; | |
}); | |
0_to(10) | |
.foreach([](auto v){ | |
cout << v <<" "<< endl; | |
}); | |
l<int>{ 1, 2, 3, 4, 5} | |
.filter([](auto i){ | |
cout<<"fitler "<< i << endl; | |
return i % 2 == 0; | |
}).map([](auto i){ | |
cout<<"map "<< i << endl; | |
return i * 2; | |
}); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment