fork(1) download
  1. #include <iostream>
  2.  
  3. namespace live_without_statements
  4. {
  5.  
  6. template <typename Action> void exec(Action action)
  7. {
  8. action();
  9. }
  10.  
  11. template <typename Action> void skip(Action action) {
  12. (void)action;
  13. }
  14.  
  15. template <typename Then>
  16. void if_op(bool cond, Then thenAction)
  17. {
  18. void (*fns[])(Then) = {&skip, &exec};
  19. fns[cond](thenAction);
  20. }
  21.  
  22. template <typename Then, typename Else>
  23. void if_op(bool cond, Then t, Else e)
  24. {
  25. if_op(cond, t);
  26. if_op(!cond, e);
  27. }
  28.  
  29. template <typename Cond, typename Body, size_t level>
  30. struct looper
  31. {
  32. static bool loop(Cond cond, Body body)
  33. {
  34. bool c = looper<Cond, Body, level-1>::loop(cond, body);
  35. if_op(c, [&c, &cond, &body](){c = looper<Cond, Body, level-1>::loop(cond, body);});
  36. return c;
  37. }
  38. };
  39.  
  40. template <typename Cond, typename Body>
  41. struct looper<Cond, Body, 0>
  42. {
  43. static bool loop(Cond cond, Body body)
  44. {
  45. bool c = cond();
  46. if_op(c, body);
  47. return c;
  48. }
  49. };
  50.  
  51. // don't worry, thermal death of the universe will happen earlier...
  52. const int level_limit = 128;
  53.  
  54. template <typename Cond, typename Body>
  55. void while_op(Cond cond, Body body)
  56. {
  57. looper<Cond, Body, level_limit>::loop(cond, body);
  58. }
  59. }
  60.  
  61. int main()
  62. {
  63. using namespace live_without_statements;
  64.  
  65. int a[] {1, 7, 3, 2, 8, 4, 2, 5, 9, 0};
  66.  
  67. size_t len = sizeof(a) / sizeof(a[0]);
  68.  
  69. size_t i = 0;
  70. while_op([&](){return i < len - 1;}, [&](){
  71. size_t j = 0;
  72. while_op([&](){return j < len - i - 1;}, [&](){
  73. if_op(a[j] > a[j+1], [&](){std::swap(a[j], a[j+1]);});
  74. ++j;
  75. });
  76. ++i;
  77. });
  78.  
  79. i = 0;
  80. while_op([&](){return i < 10;}, [&](){
  81. std::cout << "a[" << i << "] = " << a[i] << std::endl;
  82. ++i;
  83. });
  84.  
  85. return 0;
  86. }
Success #stdin #stdout 0s 3608KB
stdin
Standard input is empty
stdout
a[0] = 0
a[1] = 1
a[2] = 2
a[3] = 2
a[4] = 3
a[5] = 4
a[6] = 5
a[7] = 7
a[8] = 8
a[9] = 9