├── .gitignore ├── 0_intro └── src │ ├── colors_algorithm.cpp │ ├── cpp_algorithm.cpp │ ├── cpp_ranges.cpp │ └── unix_command.sh ├── 1_cpp11 └── src │ ├── 1.1_starting_code.cpp │ ├── 1.2_op1_concrete.cpp │ ├── 1.3_op2_generic.cpp │ ├── 1.4_op2_generic_1st_fn.cpp │ ├── 1.5_op2_mem_fn.cpp │ ├── 1.6_op2_argument_checks.cpp │ ├── 1.7_op2_throw_no_bad_returns.cpp │ ├── 1.8_op2_lookup_in_ns_and_global.cpp │ ├── 1.9_op2_parameterized_operation.cpp │ ├── 1.a_op2_multiple_args_lambda.cpp │ ├── 1.b_op2_multiple_args_bind.cpp │ └── 1.c_op2_higher_order_fns.cpp ├── 2_cpp17 └── src │ ├── 2.1_with_optional.cpp │ ├── 2.2_with_op1_generic.cpp │ └── 2.3_with_constref_overload.cpp ├── 3_cpp20 └── src │ ├── 3.1_with_invocable.cpp │ ├── 3.2_exclude_ranges.cpp │ └── 3.final_cpp20.cpp ├── 4_cpp23 └── src │ ├── 4.1_starting_code.cpp │ ├── 4.2_and_then.cpp │ ├── 4.3_transform.cpp │ ├── 4.4_or_else.cpp │ └── 4.final_cpp20.cpp ├── LICENSE ├── README.md ├── build_test.sh ├── img ├── delme └── generic-pipeline.png └── inc └── generic_pipeline.hpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | -------------------------------------------------------------------------------- /0_intro/src/colors_algorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | template 13 | bool operator<(const pair& a, const pair& b) { 14 | return a.first < b.first; 15 | } 16 | 17 | int main(int, char**) { 18 | auto& in_colors = std::cin; 19 | 20 | map counts; 21 | for_each(istream_iterator{in_colors}, istream_iterator{}, 22 | [&counts](string s) { counts[std::move(s)]++; } 23 | ); 24 | 25 | using cnt_clr = pair; 26 | vector freq; 27 | std::transform(begin(counts), end(counts), std::back_inserter(freq), 28 | [](const pair& p){ return std::make_pair(p.second, p.first); }); 29 | 30 | auto last = freq.size() > 3? begin(freq) + 3: end(freq); 31 | partial_sort(begin(freq), last, end(freq), greater{}); 32 | 33 | for_each (begin(freq), last, [](const cnt_clr& p) { 34 | cout << p.first << ' ' << p.second << '\n'; 35 | }); 36 | 37 | } -------------------------------------------------------------------------------- /0_intro/src/cpp_algorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | struct line : public std::string {}; 10 | std::istream& operator>>(std::istream& in, line& ln) { 11 | std::getline(in, ln); 12 | return in; 13 | } 14 | 15 | template 16 | std::ostream& operator<<(std::ostream& out, const std::vector& v) { 17 | for (const auto& s : v) { 18 | out << s << '\n'; 19 | } 20 | return out; 21 | } 22 | 23 | int main(int argc, char** argv) { 24 | std::vector lines; 25 | std::ifstream file(argv[1]); 26 | std::copy_if(std::istream_iterator(file), std::istream_iterator{}, 27 | std::back_inserter(lines), 28 | [](const line& ln) { return ln.find("warning: ") != std::string::npos; }); 29 | //std::cout << lines; 30 | std::sort(begin(lines), end(lines)); 31 | //std::cout << lines; 32 | auto last = std::unique(begin(lines), end(lines)); 33 | lines.erase(last, end(lines)); 34 | std::cout << lines; 35 | /*for (auto be = begin(lines); be != en; ++be) { 36 | auto found = std::adjacent_find(be, en, std::not_equal_to{}); 37 | be = lines.erase(be, found); 38 | }*/ 39 | 40 | return 0; 41 | } -------------------------------------------------------------------------------- /0_intro/src/cpp_ranges.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sankurm/generic-pipeline/1f18d21191adcae4cae7f74f2fb8a6f3ec75878b/0_intro/src/cpp_ranges.cpp -------------------------------------------------------------------------------- /0_intro/src/unix_command.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #grep "warning: " "$1" | sed -e 's/^.*warning: //1' | wc -l 3 | grep "warning: " "$1" | sed -e 's/^.*warning: //1' | uniq | sort | uniq 4 | grep "warning: " /mnt/c/Sam/UP/logs_for_ankur.txt | sed -e 's/^.*warning: //1' | uniq | sort | uniq -------------------------------------------------------------------------------- /1_cpp11/src/1.1_starting_code.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | //Pre-C++17 code without std::optional 6 | //Code relies on special values like empty string, kafka_config to be convertible to bool and return bools to determine success of a step 7 | namespace 8 | { 9 | struct env_error : public std::exception {}; 10 | struct file_error : public std::exception {}; 11 | struct json_error : public std::exception {}; 12 | struct creation_error : public std::exception {}; 13 | struct connect_error : public std::exception {}; 14 | struct subscribe_error : public std::exception {}; 15 | 16 | std::string get_env(std::string&& varname) { 17 | return "/config/kafka.json"; 18 | } 19 | 20 | std::string get_file_contents(std::string&& filename) { 21 | return "file-contents-blah-blah"; 22 | } 23 | 24 | struct kafka_config 25 | { 26 | /* url etc. */ 27 | operator bool() { return true; } 28 | }; 29 | 30 | kafka_config parse_kafka_config(std::string&& config) { 31 | return kafka_config{}; 32 | } 33 | 34 | struct kafka_consumer 35 | { 36 | kafka_consumer(const kafka_config& config) {} 37 | kafka_consumer(kafka_config&& config) {} 38 | 39 | bool connect() { return true; } 40 | bool subscribe() { return true; } 41 | 42 | operator bool() { return true; } 43 | }; 44 | 45 | kafka_consumer create_kafka_consumer(kafka_config&& config) { 46 | return kafka_consumer{std::move(config)}; 47 | } 48 | 49 | kafka_consumer init_kafka() { 50 | std::string fname = get_env("kafka-config-filename"); 51 | if (fname.empty()) { throw env_error{}; } 52 | 53 | auto contents = get_file_contents(std::move(fname)); 54 | if (contents.empty()) { throw file_error{}; } 55 | 56 | auto config = parse_kafka_config(std::move(contents)); 57 | if (!config) { throw json_error{}; } 58 | 59 | auto consumer = create_kafka_consumer(std::move(config)); 60 | if (!consumer) { throw creation_error{}; } 61 | 62 | if (!consumer.connect()) { throw connect_error{}; } 63 | if (!consumer.subscribe()) { throw subscribe_error{}; } 64 | 65 | return consumer; 66 | } 67 | } 68 | 69 | int main(int argc, char** argv) { 70 | auto consumer = init_kafka(); 71 | if (consumer) { std::cout << "Consumer creation successful\n"; } 72 | else { std::cout << "Consumer creation failed\n"; } 73 | 74 | return 0; 75 | } -------------------------------------------------------------------------------- /1_cpp11/src/1.2_op1_concrete.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using Callable = std::string(std::string&&); 7 | //auto operator|(std::string&& val, Callable fn) -> typename std::result_of::type { 8 | auto operator|(std::string&& val, Callable fn) -> std::string { 9 | return fn(std::move(val)); 10 | } 11 | 12 | //Pre-C++17 code without std::optional 13 | //Code relies on special values like empty string, kafka_config to be convertible to bool and return bools to determine success of a step 14 | namespace 15 | { 16 | struct env_error : public std::exception {}; 17 | struct file_error : public std::exception {}; 18 | struct json_error : public std::exception {}; 19 | struct creation_error : public std::exception {}; 20 | struct connect_error : public std::exception {}; 21 | struct subscribe_error : public std::exception {}; 22 | 23 | std::string get_env(std::string&& varname) { 24 | return "/config/kafka.json"; 25 | } 26 | 27 | std::string get_file_contents(std::string&& filename) { 28 | return "file-contents-blah-blah"; 29 | } 30 | 31 | struct kafka_config 32 | { 33 | /* url etc. */ 34 | operator bool() { return true; } 35 | }; 36 | 37 | kafka_config parse_kafka_config(std::string&& config) { 38 | return kafka_config{}; 39 | } 40 | 41 | struct kafka_consumer 42 | { 43 | kafka_consumer(const kafka_config& config) {} 44 | kafka_consumer(kafka_config&& config) {} 45 | 46 | bool connect() { return true; } 47 | bool subscribe() { return true; } 48 | 49 | operator bool() { return true; } 50 | }; 51 | 52 | kafka_consumer create_kafka_consumer(kafka_config&& config) { 53 | return kafka_consumer{std::move(config)}; 54 | } 55 | 56 | kafka_consumer init_kafka() { 57 | auto contents = get_env("kafka-config-filename") | get_file_contents; 58 | if (contents.empty()) { throw file_error{}; } 59 | 60 | auto config = parse_kafka_config(std::move(contents)); 61 | if (!config) { throw json_error{}; } 62 | 63 | auto consumer = create_kafka_consumer(std::move(config)); 64 | if (!consumer) { throw creation_error{}; } 65 | 66 | if (!consumer.connect()) { throw connect_error{}; } 67 | if (!consumer.subscribe()) { throw subscribe_error{}; } 68 | 69 | return consumer; 70 | } 71 | } 72 | 73 | int main(int argc, char** argv) { 74 | auto consumer = init_kafka(); 75 | if (consumer) { std::cout << "Consumer creation successful\n"; } 76 | else { std::cout << "Consumer creation failed\n"; } 77 | 78 | return 0; 79 | } -------------------------------------------------------------------------------- /1_cpp11/src/1.3_op2_generic.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | //The generic implementation also takes care of the return type of Callable being different than T 7 | //Here, parse_kafka_config(std::string&&) -> kafka_config 8 | template 9 | auto operator|(T&& val, Callable&& fn) -> typename std::result_of::type { 10 | return std::forward(fn)(std::forward(val)); 11 | } 12 | 13 | //Pre-C++17 code without std::optional 14 | //Code relies on special values like empty string, kafka_config to be convertible to bool and return bools to determine success of a step 15 | namespace 16 | { 17 | struct env_error : public std::exception {}; 18 | struct file_error : public std::exception {}; 19 | struct json_error : public std::exception {}; 20 | struct creation_error : public std::exception {}; 21 | struct connect_error : public std::exception {}; 22 | struct subscribe_error : public std::exception {}; 23 | 24 | std::string get_env(std::string&& varname) { 25 | return "/config/kafka.json"; 26 | } 27 | 28 | std::string get_file_contents(std::string&& filename) { 29 | return "file-contents-blah-blah"; 30 | } 31 | 32 | struct kafka_config 33 | { 34 | /* url etc. */ 35 | operator bool() { return true; } 36 | }; 37 | 38 | kafka_config parse_kafka_config(std::string&& config) { 39 | return kafka_config{}; 40 | } 41 | 42 | struct kafka_consumer 43 | { 44 | kafka_consumer(const kafka_config& config) {} 45 | kafka_consumer(kafka_config&& config) {} 46 | 47 | bool connect() { return true; } 48 | bool subscribe() { return true; } 49 | 50 | operator bool() { return true; } 51 | }; 52 | 53 | kafka_consumer create_kafka_consumer(kafka_config&& config) { 54 | return kafka_consumer{std::move(config)}; 55 | } 56 | 57 | kafka_consumer init_kafka() { 58 | auto config = get_env("kafka-config-filename") 59 | | get_file_contents 60 | | parse_kafka_config; 61 | if (!config) { throw json_error{}; } 62 | 63 | auto consumer = create_kafka_consumer(std::move(config)); 64 | if (!consumer) { throw creation_error{}; } 65 | 66 | if (!consumer.connect()) { throw connect_error{}; } 67 | if (!consumer.subscribe()) { throw subscribe_error{}; } 68 | 69 | return consumer; 70 | } 71 | } 72 | 73 | int main(int argc, char** argv) { 74 | auto consumer = init_kafka(); 75 | if (consumer) { std::cout << "Consumer creation successful\n"; } 76 | else { std::cout << "Consumer creation failed\n"; } 77 | 78 | return 0; 79 | } -------------------------------------------------------------------------------- /1_cpp11/src/1.4_op2_generic_1st_fn.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | //The generic implementation also takes care of the return type of Callable being different than T 7 | //Here, parse_kafka_config(std::string&&) -> kafka_config 8 | template 9 | auto operator|(T&& val, Callable&& fn) -> typename std::result_of::type { 10 | return std::forward(fn)(std::forward(val)); 11 | } 12 | 13 | //Pre-C++17 code without std::optional 14 | //Code relies on special values like empty string, kafka_config to be convertible to bool and return bools to determine success of a step 15 | namespace 16 | { 17 | struct env_error : public std::exception {}; 18 | struct file_error : public std::exception {}; 19 | struct json_error : public std::exception {}; 20 | struct creation_error : public std::exception {}; 21 | struct connect_error : public std::exception {}; 22 | struct subscribe_error : public std::exception {}; 23 | 24 | std::string get_env(std::string&& varname) { 25 | return "/config/kafka.json"; 26 | } 27 | 28 | std::string get_file_contents(std::string&& filename) { 29 | return "file-contents-blah-blah"; 30 | } 31 | 32 | struct kafka_config 33 | { 34 | /* url etc. */ 35 | operator bool() { return true; } 36 | }; 37 | 38 | kafka_config parse_kafka_config(std::string&& config) { 39 | return kafka_config{}; 40 | } 41 | 42 | struct kafka_consumer 43 | { 44 | kafka_consumer(const kafka_config& config) {} 45 | kafka_consumer(kafka_config&& config) {} 46 | 47 | bool connect() { return true; } 48 | bool subscribe() { return true; } 49 | 50 | operator bool() { return true; } 51 | }; 52 | 53 | kafka_consumer create_kafka_consumer(kafka_config&& config) { 54 | return kafka_consumer{std::move(config)}; 55 | } 56 | 57 | kafka_consumer init_kafka() { 58 | //using namespace std::string_literals; 59 | //Can use "kafka-config-filename"s as they need C++14 60 | //auto config = get_env("kafka-config-filename") 61 | auto config = std::string("kafka-config-filename") 62 | | get_env 63 | | get_file_contents 64 | | parse_kafka_config; 65 | if (!config) { throw json_error{}; } 66 | 67 | auto consumer = create_kafka_consumer(std::move(config)); 68 | if (!consumer) { throw creation_error{}; } 69 | 70 | if (!consumer.connect()) { throw connect_error{}; } 71 | if (!consumer.subscribe()) { throw subscribe_error{}; } 72 | 73 | return consumer; 74 | } 75 | } 76 | 77 | int main(int argc, char** argv) { 78 | auto consumer = init_kafka(); 79 | if (consumer) { std::cout << "Consumer creation successful\n"; } 80 | else { std::cout << "Consumer creation failed\n"; } 81 | 82 | return 0; 83 | } -------------------------------------------------------------------------------- /1_cpp11/src/1.5_op2_mem_fn.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | //The generic implementation also takes care of the return type of Callable being different than T 8 | template 9 | auto operator|(T&& val, Callable&& fn) -> typename std::result_of::type { 10 | return std::forward(fn)(std::forward(val)); 11 | } 12 | 13 | //Pre-C++17 code without std::optional 14 | //Code relies on special values like empty string, kafka_config to be convertible to bool and return bools to determine success of a step 15 | namespace 16 | { 17 | struct env_error : public std::exception {}; 18 | struct file_error : public std::exception {}; 19 | struct json_error : public std::exception {}; 20 | struct creation_error : public std::exception {}; 21 | struct connect_error : public std::exception {}; 22 | struct subscribe_error : public std::exception {}; 23 | 24 | std::string get_env(std::string&& varname) { 25 | return "/config/kafka.json"; 26 | } 27 | 28 | std::string get_file_contents(std::string&& filename) { 29 | return "file-contents-blah-blah"; 30 | } 31 | 32 | struct kafka_config 33 | { 34 | /* url etc. */ 35 | operator bool() { return true; } 36 | }; 37 | 38 | kafka_config parse_kafka_config(std::string&& config) { 39 | return kafka_config{}; 40 | } 41 | 42 | struct kafka_consumer 43 | { 44 | kafka_consumer(const kafka_config& config) {} 45 | kafka_consumer(kafka_config&& config) {} 46 | 47 | bool connect() { return true; } 48 | bool subscribe() { return true; } 49 | 50 | operator bool() { return true; } 51 | }; 52 | 53 | kafka_consumer create_kafka_consumer(kafka_config&& config) { 54 | return kafka_consumer{std::move(config)}; 55 | } 56 | 57 | kafka_consumer connect(kafka_consumer&& consumer) { 58 | consumer.connect(); 59 | return consumer; 60 | } 61 | 62 | kafka_consumer init_kafka() { 63 | return get_env("kafka-config-filename") 64 | | get_file_contents 65 | | parse_kafka_config 66 | | create_kafka_consumer 67 | | connect 68 | //| std::mem_fn(&kafka_consumer::subscribe); //mem_fn possible only if the member returns what the next step or return type needs to be 69 | | [](kafka_consumer&& consumer) { consumer.subscribe(); return consumer; }; 70 | } 71 | } 72 | 73 | int main(int argc, char** argv) { 74 | auto consumer = init_kafka(); 75 | if (consumer) { std::cout << "Consumer creation successful\n"; } 76 | else { std::cout << "Consumer creation failed\n"; } 77 | 78 | return 0; 79 | } -------------------------------------------------------------------------------- /1_cpp11/src/1.6_op2_argument_checks.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | //The generic implementation also takes care of the return type of Callable being different than T 8 | template 9 | auto operator|(T&& val, Callable&& fn) -> typename std::result_of::type { 10 | return std::forward(fn)(std::forward(val)); 11 | } 12 | 13 | //Pre-C++17 code without std::optional 14 | //Code relies on special values like empty string, kafka_config to be convertible to bool and return bools to determine success of a step 15 | namespace 16 | { 17 | struct env_error : public std::exception {}; 18 | struct file_error : public std::exception {}; 19 | struct json_error : public std::exception {}; 20 | struct creation_error : public std::exception {}; 21 | struct connect_error : public std::exception {}; 22 | struct subscribe_error : public std::exception {}; 23 | 24 | std::string get_env(std::string&& varname) { 25 | return "/config/kafka.json"; 26 | } 27 | 28 | std::string get_file_contents(std::string&& filename) { 29 | if (filename.empty()) { throw env_error{}; } 30 | return "file-contents-blah-blah"; 31 | } 32 | 33 | struct kafka_config 34 | { 35 | /* url etc. */ 36 | operator bool() { return true; } 37 | }; 38 | 39 | kafka_config parse_kafka_config(std::string&& config) { 40 | if (config.empty()) { throw file_error{}; } 41 | return kafka_config{}; 42 | } 43 | 44 | struct kafka_consumer 45 | { 46 | kafka_consumer(const kafka_config& config) {} 47 | kafka_consumer(kafka_config&& config) {} 48 | 49 | bool connect() { return true; } 50 | bool subscribe() { return true; } 51 | 52 | operator bool() { return true; } 53 | }; 54 | 55 | kafka_consumer create_kafka_consumer(kafka_config&& config) { 56 | if (!config) { throw json_error{}; } 57 | return kafka_consumer{std::move(config)}; 58 | } 59 | 60 | kafka_consumer connect(kafka_consumer&& consumer) { 61 | if (!consumer) { throw creation_error{}; } 62 | consumer.connect(); 63 | return consumer; 64 | } 65 | 66 | //All functions check before use 67 | //This is the wrong-way round - each function is assuming which is the previous function to throw the correct exception 68 | kafka_consumer init_kafka() { 69 | return get_env("kafka-config-filename") 70 | | get_file_contents 71 | | parse_kafka_config 72 | | create_kafka_consumer 73 | | connect 74 | //| std::mem_fn(&kafka_consumer::subscribe); //mem_fn possible only if the member returns what the next step or return type needs to be 75 | | [](kafka_consumer&& consumer) { 76 | if (!consumer) { throw connect_error{}; } 77 | consumer.subscribe(); 78 | return consumer; 79 | }; 80 | } 81 | } 82 | 83 | int main(int argc, char** argv) { 84 | auto consumer = init_kafka(); 85 | if (consumer) { std::cout << "Consumer creation successful\n"; } 86 | else { std::cout << "Consumer creation failed\n"; } 87 | 88 | return 0; 89 | } -------------------------------------------------------------------------------- /1_cpp11/src/1.7_op2_throw_no_bad_returns.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | //The generic implementation also takes care of the return type of Callable being different than T 9 | template 10 | auto operator|(T&& val, Callable&& fn) -> typename std::result_of::type { 11 | return std::forward(fn)(std::forward(val)); 12 | } 13 | 14 | //Pre-C++17 code without std::optional 15 | //Code relies on special values like empty string, kafka_config to be convertible to bool and return bools to determine success of a step 16 | namespace 17 | { 18 | struct env_error : public std::exception {}; 19 | struct file_error : public std::exception {}; 20 | struct json_error : public std::exception {}; 21 | struct creation_error : public std::exception {}; 22 | struct connect_error : public std::exception {}; 23 | struct subscribe_error : public std::exception {}; 24 | 25 | std::string get_env(std::string&& varname) { 26 | if (/* varname not set OR value is empty */false) { throw env_error{}; } 27 | return "/config/kafka.json"; 28 | } 29 | 30 | std::string get_file_contents(std::string&& filename) { 31 | std::ifstream file(filename, std::ios::in); 32 | if (!file) { throw file_error{}; } 33 | return "file-contents-blah-blah"; 34 | } 35 | 36 | struct kafka_config 37 | { 38 | /* url etc. */ 39 | operator bool() { return true; } 40 | }; 41 | 42 | kafka_config parse_kafka_config(std::string&& config) { 43 | if (/* parsing fails == */ false) { throw json_error{}; } 44 | return kafka_config{}; 45 | } 46 | 47 | struct kafka_consumer 48 | { 49 | kafka_consumer(const kafka_config& config) {} 50 | kafka_consumer(kafka_config&& config) {} 51 | 52 | bool connect() { return true; } 53 | bool subscribe() { return true; } 54 | 55 | operator bool() { return true; } 56 | }; 57 | 58 | kafka_consumer create_kafka_consumer(kafka_config&& config) { 59 | return kafka_consumer{std::move(config)}; 60 | } 61 | 62 | kafka_consumer connect(kafka_consumer&& consumer) { 63 | if (!consumer.connect()) { throw connect_error{}; } 64 | return consumer; 65 | } 66 | 67 | auto subscribe = [](kafka_consumer&& consumer) { 68 | if (!consumer) { throw connect_error{}; } 69 | consumer.subscribe(); 70 | return consumer; 71 | }; 72 | 73 | //All functions check before use 74 | //This is the wrong-way round - each function is assuming which is the previous function to throw the correct exception 75 | kafka_consumer init_kafka() { 76 | return get_env("kafka-config-filename") 77 | | get_file_contents 78 | | parse_kafka_config 79 | | create_kafka_consumer 80 | | connect 81 | | subscribe; 82 | } 83 | } 84 | 85 | int main(int argc, char** argv) { 86 | auto consumer = init_kafka(); 87 | if (consumer) { std::cout << "Consumer creation successful\n"; } 88 | else { std::cout << "Consumer creation failed\n"; } 89 | 90 | return 0; 91 | } -------------------------------------------------------------------------------- /1_cpp11/src/1.8_op2_lookup_in_ns_and_global.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | namespace framework 8 | { 9 | //The generic implementation cannot be put in a different namespace as it will not be considered for the parameters 10 | //Global operator is a must - and it is a potential disrupter for types that have their own overload of operator| 11 | template 12 | auto operator|(T&& val, Callable&& fn) -> typename std::result_of::type { 13 | return std::forward(fn)(std::forward(val)); 14 | } 15 | } 16 | 17 | //Pre-C++17 code without std::optional 18 | //Code relies on special values like empty string, kafka_config to be convertible to bool and return bools to determine success of a step 19 | namespace app 20 | { 21 | struct env_error : public std::exception {}; 22 | struct file_error : public std::exception {}; 23 | struct json_error : public std::exception {}; 24 | struct creation_error : public std::exception {}; 25 | struct connect_error : public std::exception {}; 26 | struct subscribe_error : public std::exception {}; 27 | 28 | std::string get_env(std::string&& varname) { 29 | return "/config/kafka.json"; 30 | } 31 | 32 | std::string get_file_contents(std::string&& filename) { 33 | if (filename.empty()) { throw env_error{}; } 34 | return "file-contents-blah-blah"; 35 | } 36 | 37 | struct kafka_config 38 | { 39 | /* url etc. */ 40 | operator bool() { return true; } 41 | }; 42 | 43 | kafka_config parse_kafka_config(std::string&& config) { 44 | if (config.empty()) { throw file_error{}; } 45 | return kafka_config{}; 46 | } 47 | 48 | struct kafka_consumer 49 | { 50 | kafka_consumer(const kafka_config& config) {} 51 | kafka_consumer(kafka_config&& config) {} 52 | 53 | bool connect() { return true; } 54 | bool subscribe() { return true; } 55 | 56 | operator bool() { return true; } 57 | }; 58 | 59 | kafka_consumer create_kafka_consumer(kafka_config&& config) { 60 | if (!config) { throw json_error{}; } 61 | return kafka_consumer{std::move(config)}; 62 | } 63 | 64 | kafka_consumer connect(kafka_consumer&& consumer) { 65 | if (!consumer) { throw connect_error{}; } 66 | consumer.connect(); 67 | return consumer; 68 | } 69 | 70 | auto subscribe = [](app::kafka_consumer&& consumer) { 71 | if (!consumer) { throw subscribe_error{}; } 72 | consumer.subscribe(); 73 | return consumer; 74 | }; 75 | } 76 | 77 | namespace framework1 78 | { 79 | //All functions check before use 80 | //This is the wrong-way round - each function is assuming which is the previous function to throw the correct exception 81 | app::kafka_consumer init_kafka() { 82 | return app::get_env("kafka-config-filename") 83 | | app::get_file_contents 84 | | app::parse_kafka_config 85 | | app::create_kafka_consumer 86 | | app::connect 87 | | app::subscribe; 88 | } 89 | } 90 | 91 | int main(int argc, char** argv) { 92 | auto consumer = framework1::init_kafka(); 93 | if (consumer) { std::cout << "Consumer creation successful\n"; } 94 | else { std::cout << "Consumer creation failed\n"; } 95 | 96 | return 0; 97 | } -------------------------------------------------------------------------------- /1_cpp11/src/1.9_op2_parameterized_operation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | //The generic implementation also takes care of the return type of Callable being different than T 9 | template 10 | auto operator|(T&& val, Callable&& fn) -> typename std::result_of::type { 11 | return std::forward(fn)(std::forward(val)); 12 | } 13 | 14 | //Pre-C++17 code without std::optional 15 | //Code relies on special values like empty string, kafka_config to be convertible to bool and return bools to determine success of a step 16 | namespace 17 | { 18 | struct env_error : public std::exception {}; 19 | struct file_error : public std::exception {}; 20 | struct json_error : public std::exception {}; 21 | struct creation_error : public std::exception {}; 22 | struct connect_error : public std::exception {}; 23 | struct subscribe_error : public std::exception {}; 24 | 25 | std::string get_env(std::string&& varname) { 26 | if (/* varname not set OR value is empty */false) { throw env_error{}; } 27 | return "/config/kafka.json"; 28 | } 29 | 30 | std::string get_file_contents(std::string&& filename) { 31 | std::ifstream file(filename, std::ios::in); 32 | if (!file && false) { throw file_error{}; } 33 | return "file-contents-blah-blah"; 34 | } 35 | 36 | struct kafka_config 37 | { 38 | /* url etc. */ 39 | operator bool() { return true; } 40 | }; 41 | 42 | enum config_type { json, xml, yaml, config_map }; 43 | 44 | template 45 | kafka_config parse_kafka_config(std::string&& config) { 46 | if (/* parsing fails == */ false) { throw json_error{}; } 47 | return kafka_config{}; 48 | } 49 | 50 | struct kafka_consumer 51 | { 52 | kafka_consumer(const kafka_config& config) {} 53 | kafka_consumer(kafka_config&& config) {} 54 | 55 | bool connect() { return true; } 56 | bool subscribe() { return true; } 57 | 58 | operator bool() { return true; } 59 | }; 60 | 61 | kafka_consumer create_kafka_consumer(kafka_config&& config) { 62 | return kafka_consumer{std::move(config)}; 63 | } 64 | 65 | kafka_consumer connect(kafka_consumer&& consumer) { 66 | if (!consumer.connect()) { throw connect_error{}; } 67 | return consumer; 68 | } 69 | 70 | auto subscribe = [](kafka_consumer&& consumer) { 71 | if (!consumer) { throw connect_error{}; } 72 | consumer.subscribe(); 73 | return consumer; 74 | }; 75 | 76 | //Parameterized function can be specified 77 | //The corresponding template shall be instantiated and used 78 | kafka_consumer init_kafka() { 79 | return get_env("kafka-config-filename") 80 | | get_file_contents 81 | | parse_kafka_config 82 | | create_kafka_consumer 83 | | connect 84 | | subscribe; 85 | } 86 | } 87 | 88 | int main(int argc, char** argv) { 89 | auto consumer = init_kafka(); 90 | if (consumer) { std::cout << "Consumer creation successful\n"; } 91 | else { std::cout << "Consumer creation failed\n"; } 92 | 93 | return 0; 94 | } -------------------------------------------------------------------------------- /1_cpp11/src/1.a_op2_multiple_args_lambda.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | //The generic implementation also takes care of the return type of Callable being different than T 9 | template 10 | auto operator|(T&& val, Callable&& fn) -> typename std::result_of::type { 11 | return std::forward(fn)(std::forward(val)); 12 | } 13 | 14 | //Pre-C++17 code without std::optional 15 | //Code relies on special values like empty string, kafka_config to be convertible to bool and return bools to determine success of a step 16 | namespace 17 | { 18 | struct env_error : public std::exception {}; 19 | struct file_error : public std::exception {}; 20 | struct json_error : public std::exception {}; 21 | struct creation_error : public std::exception {}; 22 | struct connect_error : public std::exception {}; 23 | struct subscribe_error : public std::exception {}; 24 | 25 | std::string get_env(std::string&& varname) { 26 | if (/* varname not set OR value is empty */false) { throw env_error{}; } 27 | return "/config/kafka.json"; 28 | } 29 | 30 | std::string get_file_contents(std::string&& filename) { 31 | std::ifstream file(filename, std::ios::in); 32 | if (!file && false) { throw file_error{}; } 33 | return "file-contents-blah-blah"; 34 | } 35 | 36 | struct kafka_config 37 | { 38 | /* url etc. */ 39 | operator bool() { return true; } 40 | }; 41 | 42 | enum config_type { json, xml, yaml, config_map }; 43 | 44 | template 45 | kafka_config parse_kafka_config(std::string&& config) { 46 | if (/* parsing fails == */ false) { throw json_error{}; } 47 | return kafka_config{}; 48 | } 49 | 50 | struct certificate {}; 51 | certificate get_certificate() { return certificate{}; } 52 | 53 | struct kafka_consumer 54 | { 55 | kafka_consumer(const kafka_config& config) {} 56 | kafka_consumer(kafka_config&& config) {} 57 | 58 | bool connect(const certificate&) { return true; } 59 | bool subscribe() { return true; } 60 | 61 | operator bool() { return true; } 62 | }; 63 | 64 | kafka_consumer create_kafka_consumer(kafka_config&& config) { 65 | return kafka_consumer{std::move(config)}; 66 | } 67 | 68 | kafka_consumer connect(kafka_consumer&& consumer, const certificate& cert) { 69 | if (!consumer.connect(cert)) { throw connect_error{}; } 70 | return consumer; 71 | } 72 | 73 | auto subscribe = [](kafka_consumer&& consumer) { 74 | if (!consumer) { throw subscribe_error{}; } 75 | consumer.subscribe(); 76 | return consumer; 77 | }; 78 | 79 | //Invoking an operation taking more than 1 argument 80 | //Can use a lambda - Need C++14 to be able to write this code though 81 | kafka_consumer init_kafka() { 82 | auto cert = get_certificate(); 83 | return get_env("kafka-config-filename") 84 | | get_file_contents 85 | | parse_kafka_config 86 | | create_kafka_consumer 87 | //| connect 88 | //This needs C++14 :( 89 | //| [cert = get_certificate()](kafka_consumer&& consumer) { return connect(std::move(consumer), cert); } 90 | | [&cert](kafka_consumer&& consumer) { return connect(std::move(consumer), cert); } 91 | | subscribe; 92 | } 93 | } 94 | 95 | int main(int argc, char** argv) { 96 | auto consumer = init_kafka(); 97 | if (consumer) { std::cout << "Consumer creation successful\n"; } 98 | else { std::cout << "Consumer creation failed\n"; } 99 | 100 | return 0; 101 | } -------------------------------------------------------------------------------- /1_cpp11/src/1.b_op2_multiple_args_bind.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | //The generic implementation also takes care of the return type of Callable being different than T 9 | template 10 | auto operator|(T&& val, Callable&& fn) -> typename std::result_of::type { 11 | return std::forward(fn)(std::forward(val)); 12 | } 13 | 14 | //Pre-C++17 code without std::optional 15 | //Code relies on special values like empty string, kafka_config to be convertible to bool and return bools to determine success of a step 16 | namespace 17 | { 18 | struct env_error : public std::exception {}; 19 | struct file_error : public std::exception {}; 20 | struct json_error : public std::exception {}; 21 | struct creation_error : public std::exception {}; 22 | struct connect_error : public std::exception {}; 23 | struct subscribe_error : public std::exception {}; 24 | 25 | std::string get_env(std::string&& varname) { 26 | if (/* varname not set OR value is empty */false) { throw env_error{}; } 27 | return "/config/kafka.json"; 28 | } 29 | 30 | std::string get_file_contents(std::string&& filename) { 31 | std::ifstream file(filename, std::ios::in); 32 | if (!file && false) { throw file_error{}; } 33 | return "file-contents-blah-blah"; 34 | } 35 | 36 | struct kafka_config 37 | { 38 | /* url etc. */ 39 | operator bool() { return true; } 40 | }; 41 | 42 | enum config_type { json, xml, yaml, config_map }; 43 | 44 | template 45 | kafka_config parse_kafka_config(std::string&& config) { 46 | if (/* parsing fails == */ false) { throw json_error{}; } 47 | return kafka_config{}; 48 | } 49 | 50 | struct certificate {}; 51 | certificate get_certificate() { return certificate{}; } 52 | 53 | struct kafka_consumer 54 | { 55 | kafka_consumer(const kafka_config& config) {} 56 | kafka_consumer(kafka_config&& config) {} 57 | 58 | bool connect(const certificate&) { return true; } 59 | bool subscribe() { return true; } 60 | 61 | operator bool() { return true; } 62 | }; 63 | 64 | kafka_consumer create_kafka_consumer(kafka_config&& config) { 65 | return kafka_consumer{std::move(config)}; 66 | } 67 | 68 | kafka_consumer connect(kafka_consumer&& consumer, const certificate& cert) { 69 | if (!consumer.connect(cert)) { throw connect_error{}; } 70 | return consumer; 71 | } 72 | 73 | auto subscribe = [](kafka_consumer&& consumer) { 74 | if (!consumer) { throw subscribe_error{}; } 75 | consumer.subscribe(); 76 | return consumer; 77 | }; 78 | 79 | //Invoking an operation taking more than 1 argument 80 | //std::bind solution 81 | kafka_consumer init_kafka() { 82 | return get_env("kafka-config-filename") 83 | | get_file_contents 84 | | parse_kafka_config 85 | | create_kafka_consumer 86 | //| connect 87 | | std::bind(connect, std::placeholders::_1, get_certificate()) 88 | | subscribe; 89 | } 90 | } 91 | 92 | int main(int argc, char** argv) { 93 | auto consumer = init_kafka(); 94 | if (consumer) { std::cout << "Consumer creation successful\n"; } 95 | else { std::cout << "Consumer creation failed\n"; } 96 | 97 | return 0; 98 | } -------------------------------------------------------------------------------- /1_cpp11/src/1.c_op2_higher_order_fns.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | //The generic implementation also takes care of the return type of Callable being different than T 9 | template 10 | auto operator|(T&& val, Callable&& fn) -> typename std::result_of::type { 11 | return std::forward(fn)(std::forward(val)); 12 | } 13 | 14 | //Pre-C++17 code without std::optional 15 | //Code relies on special values like empty string, kafka_config to be convertible to bool and return bools to determine success of a step 16 | namespace 17 | { 18 | struct env_error : public std::exception {}; 19 | struct file_error : public std::exception {}; 20 | struct json_error : public std::exception {}; 21 | struct creation_error : public std::exception {}; 22 | struct connect_error : public std::exception {}; 23 | struct subscribe_error : public std::exception {}; 24 | 25 | std::string get_env(std::string&& varname) { 26 | if (/* varname not set OR value is empty */false) { throw env_error{}; } 27 | return "/config/kafka.json"; 28 | } 29 | 30 | std::string get_file_contents(std::string&& filename) { 31 | std::ifstream file(filename, std::ios::in); 32 | if (!file && false) { throw file_error{}; } 33 | return "file-contents-blah-blah"; 34 | } 35 | 36 | struct kafka_config 37 | { 38 | /* url etc. */ 39 | operator bool() { return true; } 40 | }; 41 | 42 | enum config_type { json, xml, yaml, config_map }; 43 | 44 | template 45 | kafka_config parse_kafka_config(std::string&& config) { 46 | if (/* parsing fails == */ false) { throw json_error{}; } 47 | return kafka_config{}; 48 | } 49 | 50 | struct kafka_consumer 51 | { 52 | kafka_consumer(const kafka_config& config) {} 53 | kafka_consumer(kafka_config&& config) {} 54 | 55 | bool connect() { return true; } 56 | bool subscribe() { return true; } 57 | 58 | operator bool() { return true; } 59 | }; 60 | 61 | kafka_consumer create_kafka_consumer(kafka_config&& config) { 62 | return kafka_consumer{std::move(config)}; 63 | } 64 | 65 | kafka_consumer connect(kafka_consumer&& consumer) { 66 | if (!consumer.connect()) { throw connect_error{}; } 67 | return consumer; 68 | } 69 | 70 | auto subscribe = [](kafka_consumer&& consumer) { 71 | if (!consumer) { throw subscribe_error{}; } 72 | consumer.subscribe(); 73 | return consumer; 74 | }; 75 | 76 | //Invoking an operation taking more than 1 argument 77 | //std::bind solution 78 | kafka_consumer init_kafka() { 79 | //using namespace std::string_literals; 80 | //Can use "kafka-config-filename"s as they need C++14 81 | return std::string("kafka-config-filename") 82 | | get_env 83 | | get_file_contents 84 | | parse_kafka_config 85 | | create_kafka_consumer 86 | | connect 87 | | subscribe; 88 | } 89 | } 90 | 91 | int main(int argc, char** argv) { 92 | auto consumer = init_kafka(); 93 | if (consumer) { std::cout << "Consumer creation successful\n"; } 94 | else { std::cout << "Consumer creation failed\n"; } 95 | 96 | return 0; 97 | } -------------------------------------------------------------------------------- /2_cpp17/src/2.1_with_optional.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | //C++17 - disciplined code using std::optional 7 | 8 | namespace 9 | { 10 | std::optional get_env(std::string_view varname) { 11 | return "/config/kafka.json"; 12 | } 13 | 14 | std::optional get_file_contents(std::string&& filename) { 15 | return "file-contents-blah-blah"; 16 | } 17 | 18 | struct kafka_config { /* url etc. */ }; 19 | 20 | std::optional parse_kafka_config(std::string&& config) { 21 | return std::make_optional(kafka_config{}); 22 | } 23 | 24 | struct kafka_consumer 25 | { 26 | kafka_consumer(const kafka_config& config) {} 27 | kafka_consumer(kafka_config&& config) {} 28 | 29 | bool connect() { return true; } 30 | bool subscribe() { return true; } 31 | }; 32 | 33 | std::optional create_kafka_consumer(kafka_config&& config) { 34 | return std::make_optional(std::move(config)); 35 | } 36 | 37 | /*std::optional connect(kafka_consumer&& consumer) { 38 | return consumer.connect()? std::make_optional(consumer): std::nullopt; 39 | } 40 | 41 | std::optional subscribe(kafka_consumer&& consumer) { 42 | return consumer.subscribe()? std::make_optional(consumer): std::nullopt; 43 | }*/ 44 | 45 | std::optional init_kafka() { 46 | auto fname = get_env("kafka-config-filename"); 47 | if (!fname) { return std::nullopt; } 48 | 49 | auto contents = get_file_contents(std::move(fname.value())); 50 | if (!contents) { return std::nullopt; } 51 | 52 | auto config = parse_kafka_config(std::move(contents.value())); 53 | if (!config) { return std::nullopt; } 54 | 55 | auto cons = create_kafka_consumer(std::move(config.value())); 56 | if (!cons) { return std::nullopt; } 57 | 58 | auto consumer = cons.value(); 59 | if (consumer.connect() && consumer.subscribe()) { 60 | return cons; 61 | } 62 | return std::nullopt; 63 | } 64 | } 65 | 66 | int main(int argc, char** argv) { 67 | auto cons = init_kafka(); 68 | if (cons) { std::cout << "Consumer creation successful\n"; } 69 | else { std::cout << "Consumer creation failed\n"; } 70 | 71 | return 0; 72 | } -------------------------------------------------------------------------------- /2_cpp17/src/2.2_with_op1_generic.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | template 8 | auto operator|(T&& val, Callable&& fn) -> typename std::invoke_result_t { 9 | return std::invoke(std::forward(fn), std::forward(val)); 10 | } 11 | 12 | //Handle unwrapping of std::optional 13 | template 14 | auto operator|(std::optional&& opt, Callable&& fn) -> typename std::invoke_result_t { 15 | return opt? std::invoke(std::forward(fn), *std::move(opt)): std::nullopt; 16 | } 17 | 18 | //C++17 - disciplined code using std::optional 19 | namespace 20 | { 21 | std::optional get_env(std::string_view varname) { 22 | return "/config/kafka.json"; 23 | } 24 | 25 | std::optional get_file_contents(std::string&& filename) { 26 | return "file-contents-blah-blah"; 27 | } 28 | 29 | struct kafka_config { /* url etc. */ }; 30 | 31 | std::optional parse_kafka_config(std::string&& config) { 32 | return std::make_optional(kafka_config{}); 33 | } 34 | 35 | struct kafka_consumer 36 | { 37 | kafka_consumer(const kafka_config& config) {} 38 | kafka_consumer(kafka_config&& config) {} 39 | 40 | bool connect() { return true; } 41 | bool subscribe() { return true; } 42 | }; 43 | 44 | std::optional create_kafka_consumer(kafka_config&& config) { 45 | return std::make_optional(std::move(config)); 46 | } 47 | 48 | std::optional connect(kafka_consumer&& consumer) { 49 | return consumer.connect()? std::make_optional(consumer): std::nullopt; 50 | } 51 | 52 | std::optional subscribe(kafka_consumer&& consumer) { 53 | return consumer.subscribe()? std::make_optional(consumer): std::nullopt; 54 | } 55 | 56 | std::optional init_kafka() { 57 | using namespace std::string_literals; 58 | return "kafka-config-filename"s 59 | | get_env 60 | | get_file_contents 61 | | parse_kafka_config 62 | | create_kafka_consumer 63 | | connect 64 | | subscribe; 65 | } 66 | } 67 | 68 | int main(int argc, char** argv) { 69 | auto cons = init_kafka(); 70 | if (cons) { std::cout << "Consumer creation successful\n"; } 71 | else { std::cout << "Consumer creation failed\n"; } 72 | 73 | return 0; 74 | } -------------------------------------------------------------------------------- /2_cpp17/src/2.3_with_constref_overload.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | template 8 | auto operator|(T&& val, Callable&& fn) -> typename std::invoke_result_t { 9 | return std::invoke(std::forward(fn), std::forward(val)); 10 | } 11 | 12 | //Handle unwrapping of std::optional 13 | template 14 | auto operator|(std::optional&& opt, Callable&& fn) -> typename std::invoke_result_t { 15 | return opt? std::invoke(std::forward(fn), *std::move(opt)): std::nullopt; 16 | } 17 | template 18 | auto operator|(const std::optional& opt, Callable&& fn) -> typename std::invoke_result_t { 19 | return opt? std::invoke(std::forward(fn), *opt): std::nullopt; 20 | } 21 | 22 | //C++17 - disciplined code using std::optional 23 | namespace 24 | { 25 | std::optional get_env(std::string_view varname) { 26 | return "/config/kafka.json"; 27 | } 28 | 29 | std::optional get_file_contents(std::string&& filename) { 30 | return "file-contents-blah-blah"; 31 | } 32 | 33 | struct kafka_config { /* url etc. */ }; 34 | 35 | std::optional parse_kafka_config(std::string&& config) { 36 | return std::make_optional(kafka_config{}); 37 | } 38 | 39 | struct kafka_consumer 40 | { 41 | kafka_consumer(const kafka_config& config) {} 42 | kafka_consumer(kafka_config&& config) {} 43 | 44 | bool connect() const { return true; } 45 | bool subscribe() { return true; } 46 | }; 47 | 48 | std::optional create_kafka_consumer(kafka_config&& config) { 49 | return std::make_optional(std::move(config)); 50 | } 51 | /*kafka_consumer create_kafka_consumer(const kafka_config& config) { 52 | return kafka_consumer(config); 53 | }*/ 54 | 55 | std::optional connect(const kafka_consumer& consumer) { 56 | return consumer.connect()? std::make_optional(consumer): std::nullopt; 57 | } 58 | 59 | std::optional subscribe(kafka_consumer&& consumer) { 60 | return consumer.subscribe()? std::make_optional(consumer): std::nullopt; 61 | } 62 | 63 | std::optional init_kafka() { 64 | using namespace std::string_literals; 65 | auto consumer = "kafka-config-filename"s 66 | | get_env 67 | | get_file_contents 68 | | parse_kafka_config 69 | | create_kafka_consumer; 70 | return consumer | connect | subscribe; 71 | } 72 | } 73 | 74 | int main(int argc, char** argv) { 75 | auto cons = init_kafka(); 76 | if (cons) { std::cout << "Consumer creation successful\n"; } 77 | else { std::cout << "Consumer creation failed\n"; } 78 | 79 | return 0; 80 | } -------------------------------------------------------------------------------- /3_cpp20/src/3.1_with_invocable.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | template 9 | requires std::invocable 10 | auto operator|(T&& val, Callable&& fn) -> typename std::invoke_result_t { 11 | return std::invoke(std::forward(fn), std::forward(val)); 12 | } 13 | 14 | //Handle unwrapping of std::optional 15 | template 16 | requires std::invocable 17 | auto operator|(std::optional&& opt, Callable&& fn) -> typename std::invoke_result_t { 18 | return opt? std::invoke(std::forward(fn), *std::move(opt)): std::nullopt; 19 | } 20 | template 21 | requires std::invocable 22 | auto operator|(const std::optional& opt, Callable&& fn) -> typename std::invoke_result_t { 23 | return opt? std::invoke(std::forward(fn), *opt): std::nullopt; 24 | } 25 | 26 | //C++17 - disciplined code using std::optional 27 | namespace 28 | { 29 | std::optional get_env(std::string_view varname) { 30 | return "/config/kafka.json"; 31 | } 32 | 33 | std::optional get_file_contents(std::string&& filename) { 34 | return "file-contents-blah-blah"; 35 | } 36 | 37 | struct kafka_config { /* url etc. */ }; 38 | 39 | std::optional parse_kafka_config(std::string&& config) { 40 | return std::make_optional(kafka_config{}); 41 | } 42 | 43 | struct kafka_consumer 44 | { 45 | kafka_consumer(const kafka_config& config) {} 46 | kafka_consumer(kafka_config&& config) {} 47 | 48 | bool connect() const { return true; } 49 | bool subscribe() { return true; } 50 | }; 51 | 52 | std::optional create_kafka_consumer(kafka_config&& config) { 53 | return std::make_optional(std::move(config)); 54 | } 55 | /*kafka_consumer create_kafka_consumer(const kafka_config& config) { 56 | return kafka_consumer(config); 57 | }*/ 58 | 59 | std::optional connect(const kafka_consumer& consumer) { 60 | return consumer.connect()? std::make_optional(consumer): std::nullopt; 61 | } 62 | 63 | std::optional subscribe(kafka_consumer&& consumer) { 64 | return consumer.subscribe()? std::make_optional(consumer): std::nullopt; 65 | } 66 | 67 | std::optional init_kafka() { 68 | using namespace std::string_literals; 69 | auto opt_consumer = "kafka-config-filename"s 70 | | get_env 71 | | get_file_contents 72 | | parse_kafka_config 73 | | create_kafka_consumer; 74 | return opt_consumer 75 | | connect 76 | | subscribe; 77 | } 78 | } 79 | 80 | int main(int argc, char** argv) { 81 | auto cons = init_kafka(); 82 | if (cons) { std::cout << "Consumer creation successful\n"; } 83 | else { std::cout << "Consumer creation failed\n"; } 84 | 85 | return 0; 86 | } -------------------------------------------------------------------------------- /3_cpp20/src/3.2_exclude_ranges.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | template 10 | requires (not std::ranges::range && std::invocable) 11 | auto operator|(T&& val, Callable&& fn) -> typename std::invoke_result_t { 12 | return std::invoke(std::forward(fn), std::forward(val)); 13 | } 14 | 15 | //Handle unwrapping of std::optional 16 | template 17 | requires std::invocable 18 | auto operator|(std::optional&& opt, Callable&& fn) -> typename std::invoke_result_t { 19 | return opt? std::invoke(std::forward(fn), *std::move(opt)): std::nullopt; 20 | } 21 | template 22 | requires std::invocable 23 | auto operator|(const std::optional& opt, Callable&& fn) -> typename std::invoke_result_t { 24 | return opt? std::invoke(std::forward(fn), *opt): std::nullopt; 25 | } 26 | 27 | //C++17 - disciplined code using std::optional 28 | namespace 29 | { 30 | std::optional get_env(std::string_view varname) { 31 | return "/config/kafka.json"; 32 | } 33 | 34 | std::optional get_file_contents(std::string&& filename) { 35 | return "file-contents-blah-blah"; 36 | } 37 | 38 | struct kafka_config { /* url etc. */ }; 39 | 40 | std::optional parse_kafka_config(std::string&& config) { 41 | return std::make_optional(kafka_config{}); 42 | } 43 | 44 | struct kafka_consumer 45 | { 46 | kafka_consumer(const kafka_config& config) {} 47 | kafka_consumer(kafka_config&& config) {} 48 | 49 | bool connect() const { return true; } 50 | bool subscribe() const { return true; } 51 | }; 52 | 53 | std::optional create_kafka_consumer(kafka_config&& config) { 54 | return std::make_optional(std::move(config)); 55 | } 56 | /*kafka_consumer create_kafka_consumer(const kafka_config& config) { 57 | return kafka_consumer(config); 58 | }*/ 59 | 60 | std::optional connect(const kafka_consumer& consumer) { 61 | return consumer.connect()? std::make_optional(consumer): std::nullopt; 62 | } 63 | 64 | std::optional subscribe(const kafka_consumer& consumer) { 65 | return consumer.subscribe()? std::make_optional(consumer): std::nullopt; 66 | } 67 | 68 | std::optional init_kafka() { 69 | auto opt_consumer = get_env("kafka-config-filename") 70 | | get_file_contents 71 | | parse_kafka_config 72 | | create_kafka_consumer 73 | | connect; 74 | return opt_consumer | subscribe; 75 | } 76 | } 77 | 78 | int main(int argc, char** argv) { 79 | auto cons = init_kafka(); 80 | if (cons) { std::cout << "Consumer creation successful\n"; } 81 | else { std::cout << "Consumer creation failed\n"; } 82 | 83 | std::vector ints{1, 3, 5, 7, 9}; 84 | auto pipeline = ints | std::views::transform([](int n) { return n*n; }); 85 | for (auto n : pipeline) { std::cout << n << ' '; } 86 | std::cout << '\n'; 87 | 88 | return 0; 89 | } -------------------------------------------------------------------------------- /3_cpp20/src/3.final_cpp20.cpp: -------------------------------------------------------------------------------- 1 | #include "inc/generic_pipeline.hpp" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | //C++17 - disciplined code using std::optional 12 | namespace 13 | { 14 | std::optional get_env(std::string_view varname) { 15 | return "/config/kafka.json"; 16 | } 17 | 18 | std::optional get_file_contents(std::string&& filename) { 19 | return "file-contents-blah-blah"; 20 | } 21 | 22 | struct kafka_config { /* url etc. */ }; 23 | 24 | std::optional parse_kafka_config(std::string&& config) { 25 | return std::make_optional(kafka_config{}); 26 | } 27 | 28 | struct kafka_consumer 29 | { 30 | kafka_consumer(const kafka_config& config) {} 31 | kafka_consumer(kafka_config&& config) {} 32 | 33 | bool connect() const { return true; } 34 | bool subscribe() const { return true; } 35 | }; 36 | 37 | std::optional create_kafka_consumer(kafka_config&& config) { 38 | return std::make_optional(std::move(config)); 39 | } 40 | /*kafka_consumer create_kafka_consumer(const kafka_config& config) { 41 | return kafka_consumer(config); 42 | }*/ 43 | 44 | std::optional connect(const kafka_consumer& consumer) { 45 | return consumer.connect()? std::make_optional(consumer): std::nullopt; 46 | } 47 | 48 | std::optional subscribe(const kafka_consumer& consumer) { 49 | return consumer.subscribe()? std::make_optional(consumer): std::nullopt; 50 | } 51 | 52 | std::optional init_kafka() { 53 | auto opt_consumer = get_env("kafka-config-filename") 54 | | get_file_contents 55 | | parse_kafka_config 56 | | create_kafka_consumer 57 | | connect; 58 | return opt_consumer | subscribe; 59 | } 60 | } 61 | 62 | int main(int argc, char** argv) { 63 | auto cons = init_kafka(); 64 | if (cons) { std::cout << "Consumer creation successful\n"; } 65 | else { std::cout << "Consumer creation failed\n"; } 66 | 67 | std::vector ints{1, 3, 5, 7, 9}; 68 | auto pipeline = ints | std::views::transform([](int n) { return n*n; }); 69 | for (auto n : pipeline) { std::cout << n << ' '; } 70 | std::cout << '\n'; 71 | 72 | return 0; 73 | } -------------------------------------------------------------------------------- /4_cpp23/src/4.1_starting_code.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | template 11 | requires (not std::ranges::range && std::invocable) 12 | auto operator|(T&& val, Callable&& fn) -> typename std::invoke_result_t { 13 | return std::invoke(std::forward(fn), std::forward(val)); 14 | } 15 | 16 | //This is the overload that handles the and_then case 17 | template 18 | requires std::invocable 19 | auto operator|(std::optional&& opt, Callable&& fn) -> typename std::invoke_result_t { 20 | return opt? std::invoke(std::forward(fn), *std::move(opt)): std::nullopt; 21 | } 22 | template 23 | requires std::invocable 24 | auto operator|(const std::optional& opt, Callable&& fn) -> typename std::invoke_result_t { 25 | return opt? std::invoke(std::forward(fn), *opt): std::nullopt; 26 | } 27 | 28 | //C++17 - disciplined code using std::optional 29 | namespace 30 | { 31 | std::optional get_env(std::string_view varname) { 32 | return "/config/kafka.json"; 33 | } 34 | 35 | std::optional get_file_contents(std::string&& filename) { 36 | return "file-contents-blah-blah"; 37 | } 38 | 39 | struct kafka_config { /* url etc. */ }; 40 | 41 | std::optional parse_kafka_config(std::string&& config) { 42 | return std::make_optional(kafka_config{}); 43 | } 44 | 45 | struct kafka_consumer 46 | { 47 | kafka_consumer(const kafka_config& config) {} 48 | kafka_consumer(kafka_config&& config) {} 49 | 50 | bool connect() const { return true; } 51 | bool subscribe() { return true; } 52 | }; 53 | 54 | std::optional create_kafka_consumer(kafka_config&& config) { 55 | return std::make_optional(std::move(config)); 56 | } 57 | /*kafka_consumer create_kafka_consumer(const kafka_config& config) { 58 | return kafka_consumer(config); 59 | }*/ 60 | 61 | std::optional connect(const kafka_consumer& consumer) { 62 | return consumer.connect()? std::make_optional(consumer): std::nullopt; 63 | } 64 | 65 | std::optional subscribe(kafka_consumer&& consumer) { 66 | return consumer.subscribe()? std::make_optional(consumer): std::nullopt; 67 | } 68 | 69 | std::optional init_kafka() { 70 | return get_env("kafka-config-filename") 71 | | get_file_contents 72 | | parse_kafka_config 73 | | create_kafka_consumer 74 | | connect 75 | | subscribe; 76 | /*return get_env("kafka-config-filename") 77 | .and_then(get_file_contents) 78 | .transform(parse_kafka_config) 79 | .transform(create_kafka_consumer) 80 | .and_then(connect) 81 | .and_then(subscribe);*/ 82 | } 83 | } 84 | 85 | int main(int argc, char** argv) { 86 | auto cons = init_kafka(); 87 | if (cons) { std::cout << "Consumer creation successful\n"; } 88 | else { std::cout << "Consumer creation failed\n"; } 89 | 90 | std::vector ints{1, 3, 5, 7, 9}; 91 | auto pipeline = ints | std::views::transform([](int n) { return n*n; }); 92 | for (auto n : pipeline) { std::cout << n << ' '; } 93 | std::cout << '\n'; 94 | 95 | return 0; 96 | } -------------------------------------------------------------------------------- /4_cpp23/src/4.2_and_then.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | /* 11 | template 12 | concept basic_optional = requires (T t) { 13 | typename T::value_type; 14 | std::convertible_to; 15 | std::same_as, typename T::value_type>; 16 | std::constructible_from; 17 | }; 18 | static_assert(basic_optional>); 19 | */ 20 | 21 | namespace internal 22 | { 23 | template 24 | struct is_std_optional : std::false_type {}; 25 | 26 | template 27 | struct is_std_optional> : std::true_type {}; 28 | 29 | template 30 | const bool is_std_optional_v = is_std_optional::value; 31 | 32 | template 33 | concept invocable_returns_std_optional = std::invocable && is_std_optional_v>; 34 | 35 | template 36 | concept invocable_returns_not_std_optional = std::invocable && !is_std_optional_v>; 37 | } 38 | 39 | template 40 | requires (not std::ranges::range && std::invocable) 41 | auto operator|(T&& val, Callable&& fn) -> typename std::invoke_result_t { 42 | return std::invoke(std::forward(fn), std::forward(val)); 43 | } 44 | 45 | //These overloads handle std::optional::and_then case 46 | template 47 | requires internal::invocable_returns_std_optional 48 | auto operator|(std::optional&& opt, Callable&& fn) -> typename std::invoke_result_t { 49 | return opt? std::invoke(std::forward(fn), *std::move(opt)): std::nullopt; 50 | } 51 | template 52 | requires internal::invocable_returns_std_optional 53 | auto operator|(const std::optional& opt, Callable&& fn) -> typename std::invoke_result_t { 54 | return opt? std::invoke(std::forward(fn), *opt): std::nullopt; 55 | } 56 | 57 | //C++17 - disciplined code using std::optional 58 | namespace 59 | { 60 | std::optional get_env(std::string_view varname) { 61 | return "/config/kafka.json"; 62 | } 63 | 64 | std::optional get_file_contents(std::string&& filename) { 65 | return "file-contents-blah-blah"; 66 | } 67 | 68 | struct kafka_config { /* url etc. */ }; 69 | 70 | std::optional parse_kafka_config(std::string&& config) { 71 | return std::make_optional(kafka_config{}); 72 | } 73 | 74 | struct kafka_consumer 75 | { 76 | kafka_consumer(const kafka_config& config) {} 77 | kafka_consumer(kafka_config&& config) {} 78 | 79 | bool connect() const { return true; } 80 | bool subscribe() { return true; } 81 | }; 82 | 83 | std::optional create_kafka_consumer(kafka_config&& config) { 84 | return std::make_optional(std::move(config)); 85 | } 86 | /*kafka_consumer create_kafka_consumer(const kafka_config& config) { 87 | return kafka_consumer(config); 88 | }*/ 89 | 90 | std::optional connect(const kafka_consumer& consumer) { 91 | return consumer.connect()? std::make_optional(consumer): std::nullopt; 92 | } 93 | 94 | std::optional subscribe(kafka_consumer&& consumer) { 95 | return consumer.subscribe()? std::make_optional(consumer): std::nullopt; 96 | } 97 | 98 | std::optional init_kafka() { 99 | auto opt_consumer = get_env("kafka-config-filename") 100 | | get_file_contents 101 | | parse_kafka_config 102 | | create_kafka_consumer; 103 | return opt_consumer 104 | | connect 105 | | subscribe; 106 | } 107 | } 108 | 109 | int main(int argc, char** argv) { 110 | auto cons = init_kafka(); 111 | if (cons) { std::cout << "Consumer creation successful\n"; } 112 | else { std::cout << "Consumer creation failed\n"; } 113 | 114 | std::vector ints{1, 3, 5, 7, 9}; 115 | auto pipeline = ints | std::views::transform([](int n) { return n*n; }); 116 | for (auto n : pipeline) { std::cout << n << ' '; } 117 | std::cout << '\n'; 118 | 119 | return 0; 120 | } -------------------------------------------------------------------------------- /4_cpp23/src/4.3_transform.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | template 11 | concept basic_optional = requires (T t) { 12 | typename T::value_type; 13 | std::convertible_to; 14 | std::same_as, typename T::value_type>; 15 | std::constructible_from; 16 | }; 17 | static_assert(basic_optional>); 18 | 19 | template 20 | requires (not std::ranges::range && std::invocable) 21 | auto operator|(T&& val, Callable&& fn) -> typename std::invoke_result_t { 22 | return std::invoke(std::forward(fn), std::forward(val)); 23 | } 24 | 25 | //These overloads handle std::optional::and_then case 26 | template 27 | requires std::invocable && basic_optional> 28 | auto operator|(std::optional&& opt, Callable&& fn) -> typename std::invoke_result_t { 29 | return opt? std::invoke(std::forward(fn), *std::move(opt)): std::nullopt; 30 | } 31 | template 32 | requires std::invocable && basic_optional> 33 | auto operator|(const std::optional& opt, Callable&& fn) -> typename std::invoke_result_t { 34 | return opt? std::invoke(std::forward(fn), *opt): std::nullopt; 35 | } 36 | 37 | //These overloads handle std::optional::transform case 38 | template 39 | requires std::invocable && (!basic_optional>) 40 | auto operator|(std::optional&& opt, Callable&& fn) -> std::optional> { 41 | return opt? std::make_optional(std::invoke(std::forward(fn), *std::move(opt))): std::nullopt; 42 | } 43 | template 44 | requires std::invocable && (!basic_optional>) 45 | auto operator|(const std::optional& opt, Callable&& fn) -> std::optional> { 46 | return opt? std::make_optional(std::invoke(std::forward(fn), *opt)): std::nullopt; 47 | } 48 | 49 | //C++17 - disciplined code using std::optional 50 | namespace 51 | { 52 | std::optional get_env(std::string_view varname) { 53 | return "/config/kafka.json"; 54 | } 55 | 56 | std::string get_file_contents(std::string&& filename) { 57 | return "file-contents-blah-blah"; 58 | } 59 | 60 | struct kafka_config { /* url etc. */ }; 61 | 62 | /*std::optional parse_kafka_config(std::string&& config) { 63 | return std::make_optional(kafka_config{}); 64 | }*/ 65 | kafka_config parse_kafka_config(std::string&& config) { 66 | return kafka_config{}; 67 | } 68 | 69 | struct kafka_consumer 70 | { 71 | kafka_consumer(const kafka_config& config) {} 72 | kafka_consumer(kafka_config&& config) {} 73 | 74 | bool connect() const { return true; } 75 | bool subscribe() { return true; } 76 | }; 77 | 78 | /*std::optional create_kafka_consumer(kafka_config&& config) { 79 | return std::make_optional(std::move(config)); 80 | }*/ 81 | kafka_consumer create_kafka_consumer(const kafka_config& config) { 82 | return kafka_consumer(config); 83 | } 84 | 85 | std::optional connect(const kafka_consumer& consumer) { 86 | return consumer.connect()? std::make_optional(consumer): std::nullopt; 87 | } 88 | 89 | std::optional subscribe(kafka_consumer&& consumer) { 90 | return consumer.subscribe()? std::make_optional(consumer): std::nullopt; 91 | } 92 | 93 | std::optional init_kafka() { 94 | auto opt_consumer = get_env("kafka-config-filename") 95 | | get_file_contents 96 | | parse_kafka_config 97 | | create_kafka_consumer; 98 | return opt_consumer 99 | | connect 100 | | subscribe; 101 | } 102 | } 103 | 104 | int main(int argc, char** argv) { 105 | auto cons = init_kafka(); 106 | if (cons) { std::cout << "Consumer creation successful\n"; } 107 | else { std::cout << "Consumer creation failed\n"; } 108 | 109 | std::vector ints{1, 3, 5, 7, 9}; 110 | auto pipeline = ints | std::views::transform([](int n) { return n*n; }); 111 | for (auto n : pipeline) { std::cout << n << ' '; } 112 | std::cout << '\n'; 113 | 114 | return 0; 115 | } -------------------------------------------------------------------------------- /4_cpp23/src/4.4_or_else.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | template 12 | concept basic_optional = requires (T t) { 13 | typename T::value_type; 14 | std::convertible_to; 15 | std::same_as, typename T::value_type>; 16 | std::constructible_from; 17 | }; 18 | static_assert(basic_optional>); 19 | 20 | template 21 | requires (not std::ranges::range && std::invocable) 22 | auto operator|(T&& val, Callable&& fn) -> typename std::invoke_result_t { 23 | return std::invoke(std::forward(fn), std::forward(val)); 24 | } 25 | 26 | //These overloads handle std::optional::and_then case 27 | template 28 | requires std::invocable && basic_optional> 29 | auto operator|(std::optional&& opt, Callable&& fn) -> typename std::invoke_result_t { 30 | return opt? std::invoke(std::forward(fn), *std::move(opt)): std::nullopt; 31 | } 32 | template 33 | requires std::invocable && basic_optional> 34 | auto operator|(const std::optional& opt, Callable&& fn) -> typename std::invoke_result_t { 35 | return opt? std::invoke(std::forward(fn), *opt): std::nullopt; 36 | } 37 | 38 | //These overloads handle std::optional::transform case 39 | template 40 | requires std::invocable && (!basic_optional>) 41 | auto operator|(std::optional&& opt, Callable&& fn) -> std::optional> { 42 | return opt? std::make_optional(std::invoke(std::forward(fn), *std::move(opt))): std::nullopt; 43 | } 44 | template 45 | requires std::invocable && (!basic_optional>) 46 | auto operator|(const std::optional& opt, Callable&& fn) -> std::optional> { 47 | return opt? std::make_optional(std::invoke(std::forward(fn), *opt)): std::nullopt; 48 | } 49 | 50 | //These overloads handle std::optional::or_else case 51 | template 52 | requires (!std::invocable) && std::invocable && std::same_as, std::optional> 53 | auto operator|(std::optional&& opt, Callable&& fn) -> typename std::invoke_result_t { 54 | return opt? *opt: std::invoke(std::forward(fn)); 55 | } 56 | template 57 | requires (!std::invocable) && std::invocable && std::same_as, std::optional> 58 | auto operator|(const std::optional& opt, Callable&& fn) -> typename std::invoke_result_t { 59 | return opt? *opt: std::invoke(std::forward(fn)); 60 | } 61 | 62 | //C++17 - disciplined code using std::optional 63 | namespace 64 | { 65 | std::optional get_env(std::string_view varname) { 66 | return "/config/kafka.json"; 67 | } 68 | 69 | /*std::string get_file_contents(std::string&& filename) { 70 | return "file-contents-blah-blah"; 71 | }*/ 72 | std::optional get_file_contents(std::string&& filename) { 73 | std::ifstream file(filename, std::ios::in); 74 | if (!file) { return std::nullopt; } 75 | return "file-contents-blah-blah"; 76 | } 77 | std::optional get_default_config() { 78 | return "default-contents"; 79 | } 80 | 81 | struct kafka_config { /* url etc. */ }; 82 | 83 | /*std::optional parse_kafka_config(std::string&& config) { 84 | return std::make_optional(kafka_config{}); 85 | }*/ 86 | kafka_config parse_kafka_config(std::string&& config) { 87 | return kafka_config{}; 88 | } 89 | 90 | struct kafka_consumer 91 | { 92 | kafka_consumer(const kafka_config& config) {} 93 | kafka_consumer(kafka_config&& config) {} 94 | 95 | bool connect() const { return true; } 96 | bool subscribe() { return true; } 97 | }; 98 | 99 | /*std::optional create_kafka_consumer(kafka_config&& config) { 100 | return std::make_optional(std::move(config)); 101 | }*/ 102 | kafka_consumer create_kafka_consumer(const kafka_config& config) { 103 | return kafka_consumer(config); 104 | } 105 | 106 | std::optional connect(const kafka_consumer& consumer) { 107 | return consumer.connect()? std::make_optional(consumer): std::nullopt; 108 | } 109 | 110 | std::optional subscribe(kafka_consumer&& consumer) { 111 | return consumer.subscribe()? std::make_optional(consumer): std::nullopt; 112 | } 113 | 114 | std::optional init_kafka() { 115 | return get_env("kafka-config-filename") 116 | | get_file_contents | get_default_config 117 | | parse_kafka_config 118 | | create_kafka_consumer 119 | | connect 120 | | subscribe; 121 | } 122 | } 123 | 124 | int main(int argc, char** argv) { 125 | auto cons = init_kafka(); 126 | if (cons) { std::cout << "Consumer creation successful\n"; } 127 | else { std::cout << "Consumer creation failed\n"; } 128 | 129 | std::vector ints{1, 3, 5, 7, 9}; 130 | auto pipeline = ints | std::views::transform([](int n) { return n*n; }); 131 | for (auto n : pipeline) { std::cout << n << ' '; } 132 | std::cout << '\n'; 133 | 134 | [[maybe_unused]] auto dummy = get_default_config(); 135 | return 0; 136 | } -------------------------------------------------------------------------------- /4_cpp23/src/4.final_cpp20.cpp: -------------------------------------------------------------------------------- 1 | #include "inc/generic_pipeline.hpp" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | //C++17 - disciplined code using std::optional 14 | namespace 15 | { 16 | std::optional get_env(std::string_view varname) { 17 | return "/config/kafka.json"; 18 | } 19 | 20 | /*std::string get_file_contents(std::string&& filename) { 21 | return "file-contents-blah-blah"; 22 | }*/ 23 | std::optional get_file_contents(std::string&& filename) { 24 | std::ifstream file(filename, std::ios::in); 25 | if (!file) { return std::nullopt; } 26 | return "file-contents-blah-blah"; 27 | } 28 | std::optional get_default_config() { 29 | return "default-contents"; 30 | } 31 | 32 | struct kafka_config { /* url etc. */ }; 33 | 34 | /*std::optional parse_kafka_config(std::string&& config) { 35 | return std::make_optional(kafka_config{}); 36 | }*/ 37 | kafka_config parse_kafka_config(std::string&& config) { 38 | return kafka_config{}; 39 | } 40 | 41 | struct kafka_consumer 42 | { 43 | kafka_consumer(const kafka_config& config) {} 44 | kafka_consumer(kafka_config&& config) {} 45 | 46 | bool connect() const { return true; } 47 | bool subscribe() { return true; } 48 | }; 49 | 50 | /*std::optional create_kafka_consumer(kafka_config&& config) { 51 | return std::make_optional(std::move(config)); 52 | }*/ 53 | kafka_consumer create_kafka_consumer(const kafka_config& config) { 54 | return kafka_consumer(config); 55 | } 56 | 57 | std::optional connect(const kafka_consumer& consumer) { 58 | return consumer.connect()? std::make_optional(consumer): std::nullopt; 59 | } 60 | 61 | std::optional subscribe(kafka_consumer&& consumer) { 62 | return consumer.subscribe()? std::make_optional(consumer): std::nullopt; 63 | } 64 | 65 | std::optional init_kafka() { 66 | return get_env("kafka-config-filename") 67 | | get_file_contents | get_default_config 68 | | parse_kafka_config 69 | | create_kafka_consumer 70 | | connect 71 | | subscribe; 72 | } 73 | } 74 | 75 | int main(int argc, char** argv) { 76 | auto cons = init_kafka(); 77 | if (cons) { std::cout << "Consumer creation successful\n"; } 78 | else { std::cout << "Consumer creation failed\n"; } 79 | 80 | std::vector ints{1, 3, 5, 7, 9}; 81 | auto pipeline = ints | std::views::transform([](int n) { return n*n; }); 82 | for (auto n : pipeline) { std::cout << n << ' '; } 83 | std::cout << '\n'; 84 | 85 | [[maybe_unused]] auto dummy = get_default_config(); 86 | return 0; 87 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Ankur Satle 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # generic-pipeline 2 | A C++ repository to pipe operations functional and Unix style! 3 | 4 | Header-only. Dive right in: [generic_pipeline.hpp](/inc/generic_pipeline.hpp). 5 | 6 | ![Code snippet showing the simplicity achieved by genreic pipeline](./img/generic-pipeline.png "Simplicity using generic-pipeline") 7 | 8 | ## The Generic Pipeline 9 | All the pipeline code is in the [generic_pipeline.hpp](/inc/generic_pipeline.hpp). Jump straight to it to the usable pipelines. This currently compiles with `-std=c++20`. I shall be updating the code to add conditional constructs to enable backward compatability. 10 | 11 | The repository is organised into sections as per the coverage of my talk: 12 | 13 | ## Section 0 - Unix pipelines & C++ journey from `` to `` 14 | Introduction and initial code snippets 15 | 16 | ## Section 1 - C++11 17 | Sequentially building the solution. Follow files from `1.1_*.cpp` to `1.c_*.cpp`. 18 | 19 | ## Section 2 - C++17 20 | Use of `std::optional` and handling thereof - unpacking & packing. 21 | 22 | ## Section 3 - C++20 23 | Using `concept`s to make sure `ranges` pipelines are not impacted by generic-pipeline. 24 | 25 | ## Section 4 - C++23 26 | Improvise and make concise the continuations of the `std::optional` monadic interface introduced in C++23. 27 | 28 | ## Compiling & testing 29 | Steps are: 30 | 31 | ```sh 32 | $ git clone git@github.com:sankurm/generic-pipeline.git 33 | 34 | $ mkdir build 35 | $ cd build 36 | 37 | # To compile the file 1.2_op1_concrete.cpp in directory 1_cpp11/src: 38 | $ ls ../1_cpp11/src/1.2_op1_concrete.cpp 39 | ../1_cpp11/src/1.2_op1_concrete.cpp 40 | $ ../build_test.sh 1.2 41 | 42 | # Similarly, to compile file 2.3_with_constref_overload.cpp inside directory 2_cpp17/src: 43 | $ ls ../2_cpp17/src/2.3_with_constref_overload.cpp 44 | ../2_cpp17/src/2.3_with_constref_overload.cpp 45 | $ ../build_test.sh 2.3 46 | 47 | # Syntax is 48 | $ ../build_test.sh 49 | ``` 50 | -------------------------------------------------------------------------------- /build_test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | PREFIX="$1" 4 | 5 | DIR=`echo ${PREFIX} | cut -d'.' -f1` 6 | FILE=`echo ${PREFIX} | cut -d'.' -f2` 7 | 8 | HOME=.. 9 | INC="-I ${HOME}" 10 | 11 | if [ ${DIR} -eq 1 ]; then 12 | CXXSTD="-std=c++11" 13 | elif [ ${DIR} -eq 2 ]; then 14 | CXXSTD="-std=c++17" 15 | elif [ ${DIR} -eq 3 ]; then 16 | CXXSTD="-std=c++20" 17 | elif [ ${DIR} -eq 4 ]; then 18 | CXXSTD="-std=c++20" 19 | fi 20 | 21 | 22 | #Compile 23 | echo -e "\033[0;33m$ g++ ${CXXSTD} -Wall -Werror ${INC} ${HOME}/${DIR}_*/src/${PREFIX}_*.cpp -o ./${PREFIX}.out\033[0m" 24 | g++ ${CXXSTD} -Wall -Werror ${INC} ${HOME}/${DIR}_*/src/${PREFIX}_*.cpp -o ./${PREFIX}.out 25 | 26 | status=$? 27 | if [ ${status} -ne 0 ]; then 28 | exit ${status} 29 | fi 30 | 31 | #Run 32 | echo -e "\033[0;33m$ ./${PREFIX}.out\033[0m" 33 | ./${PREFIX}.out 34 | -------------------------------------------------------------------------------- /img/delme: -------------------------------------------------------------------------------- 1 | Nothing to write 2 | -------------------------------------------------------------------------------- /img/generic-pipeline.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sankurm/generic-pipeline/1f18d21191adcae4cae7f74f2fb8a6f3ec75878b/img/generic-pipeline.png -------------------------------------------------------------------------------- /inc/generic_pipeline.hpp: -------------------------------------------------------------------------------- 1 | #ifndef GENERIC_PIPELINE_HPP 2 | #define GENERIC_PIPELINE_HPP 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | template 11 | concept basic_optional = requires (T t) { 12 | typename T::value_type; 13 | std::convertible_to; 14 | std::same_as, typename T::value_type>; 15 | std::constructible_from; 16 | }; 17 | static_assert(basic_optional>); 18 | 19 | template 20 | requires (not std::ranges::range && std::invocable) 21 | auto operator|(T&& val, Callable&& fn) -> typename std::invoke_result_t { 22 | return std::invoke(std::forward(fn), std::forward(val)); 23 | } 24 | 25 | //These overloads handle std::optional::and_then case 26 | template 27 | requires std::invocable && basic_optional> 28 | auto operator|(std::optional&& opt, Callable&& fn) -> typename std::invoke_result_t { 29 | return opt? std::invoke(std::forward(fn), *std::move(opt)): std::nullopt; 30 | } 31 | template 32 | requires std::invocable && basic_optional> 33 | auto operator|(const std::optional& opt, Callable&& fn) -> typename std::invoke_result_t { 34 | return opt? std::invoke(std::forward(fn), *opt): std::nullopt; 35 | } 36 | 37 | //These overloads handle std::optional::transform case 38 | template 39 | requires std::invocable && (!basic_optional>) 40 | auto operator|(std::optional&& opt, Callable&& fn) -> std::optional> { 41 | return opt? std::make_optional(std::invoke(std::forward(fn), *std::move(opt))): std::nullopt; 42 | } 43 | template 44 | requires std::invocable && (!basic_optional>) 45 | auto operator|(const std::optional& opt, Callable&& fn) -> std::optional> { 46 | return opt? std::make_optional(std::invoke(std::forward(fn), *opt)): std::nullopt; 47 | } 48 | 49 | //These overloads handle std::optional::or_else case 50 | template 51 | requires (!std::invocable) && std::invocable && std::same_as, std::optional> 52 | auto operator|(std::optional&& opt, Callable&& fn) -> typename std::invoke_result_t { 53 | return opt? *opt: std::invoke(std::forward(fn)); 54 | } 55 | template 56 | requires (!std::invocable) && std::invocable && std::same_as, std::optional> 57 | auto operator|(const std::optional& opt, Callable&& fn) -> typename std::invoke_result_t { 58 | return opt? *opt: std::invoke(std::forward(fn)); 59 | } 60 | 61 | #endif //GENERIC_PIPELINE_HPP --------------------------------------------------------------------------------