fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. template<class...Ts>
  4. struct sink:std::function<void(Ts...)>{
  5. using std::function<void(Ts...)>::function;
  6. };
  7. template<class...Ts>
  8. using source=sink<sink<Ts...>>;
  9. template<class In, class Out>
  10. using process=sink< source<In>, sink<Out> >;
  11. template<class In, class Out>
  12. sink<In> operator|( process< In, Out > a, sink< Out > b ){
  13. return [a,b]( In in ){
  14. a( [&in]( sink<In> s )mutable{ s(std::forward<In>(in)); }, b );
  15. };
  16. }
  17. template<class In, class Out>
  18. source<Out> operator|( source< In > a, process< In, Out > b ){
  19. return [a,b]( sink<Out> out ){
  20. b( a, out );
  21. };
  22. }
  23.  
  24. template<class In, class Mid, class Out>
  25. process<In, Out> operator|( process<In, Mid> a, process<Mid, Out> b ){
  26. return [a,b]( source<In> in, sink<Out> out ){
  27. a( in, b|out ); // or b( in|a, out )
  28. };
  29. }
  30. template<class...Ts>
  31. sink<> operator|( source<Ts...> a, sink<Ts...> b ){
  32. return[a,b]{ a(b); };
  33. }
  34. process<char, char> to_upper = []( source<char> in, sink<char> out ){
  35. in( [&out]( char c ) { out( std::toupper(c) ); } );
  36. };
  37. process<char, char> with_new_line = []( source<char> in, sink<char> out ){
  38. in( [&out]( char c ) { out(c); } );
  39. };
  40. source<char> hello_world = [ptr="hello world"]( sink<char> s ){
  41. for (auto it = ptr; *it; ++it ){ s(*it); }
  42. };
  43. sink<char> print = [](char c){std::cout<<c;};
  44.  
  45. int main(){
  46. auto prog = hello_world|to_upper|with_new_line|print;
  47. prog();
  48. /*string s="hello world";
  49.   for(auto&c:s){
  50.   auto out=std::toupper(c);string t;t.push_back(out);
  51.   cout<<t<<'\n';
  52.   }*/
  53. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
HELLO WORLD