From 26e9b317ecc7219f4599a3084f12b56e39d7f0d7 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Fri, 15 Mar 2024 10:28:47 +0100 Subject: [PATCH 1/2] chore: add functor --- Functor/CMakeLists.txt | 21 +++++++++++++++++++++ Functor/bootstrap.sh | 1 + Functor/build.sh | 1 + Functor/main.cpp | 8 ++++++++ 4 files changed, 31 insertions(+) create mode 100644 Functor/CMakeLists.txt create mode 120000 Functor/bootstrap.sh create mode 120000 Functor/build.sh create mode 100644 Functor/main.cpp diff --git a/Functor/CMakeLists.txt b/Functor/CMakeLists.txt new file mode 100644 index 0000000..b99f5ca --- /dev/null +++ b/Functor/CMakeLists.txt @@ -0,0 +1,21 @@ +cmake_minimum_required(VERSION 3.20) + +project( + Functor + VERSION 0.1.0 + LANGUAGES CXX) + +list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/../cmake") + +include(ExportCompileCommands) +include(sccache) + +add_executable(Functor main.cpp) + +target_compile_features(Functor PUBLIC cxx_std_20) + +set_target_properties( + Functor + PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/bin" + RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/bin" + RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/bin") diff --git a/Functor/bootstrap.sh b/Functor/bootstrap.sh new file mode 120000 index 0000000..796ed13 --- /dev/null +++ b/Functor/bootstrap.sh @@ -0,0 +1 @@ +../scripts/bootstrap.sh \ No newline at end of file diff --git a/Functor/build.sh b/Functor/build.sh new file mode 120000 index 0000000..0eff50d --- /dev/null +++ b/Functor/build.sh @@ -0,0 +1 @@ +../scripts/build.sh \ No newline at end of file diff --git a/Functor/main.cpp b/Functor/main.cpp new file mode 100644 index 0000000..f25326b --- /dev/null +++ b/Functor/main.cpp @@ -0,0 +1,8 @@ +#include + +int main(int argc, char *argv[]) { + + std::cout << "Functor" << std::endl; + + return EXIT_SUCCESS; +} From 659e010a7093d501c36f51bc2781151e6593d581 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Fri, 15 Mar 2024 10:39:42 +0100 Subject: [PATCH 2/2] feat: implement join-string functor --- Functor/main.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/Functor/main.cpp b/Functor/main.cpp index f25326b..c771ff8 100644 --- a/Functor/main.cpp +++ b/Functor/main.cpp @@ -1,8 +1,39 @@ #include +#include +#include + +class join_strings_functor { +private: + std::string separator_ = " "; + +public: + join_strings_functor() = default; + join_strings_functor(std::string_view separator) : separator_(separator) {} + + std::string operator()(const std::string &left, + const std::string &right) const { + return left + separator_ + right; + } +}; int main(int argc, char *argv[]) { std::cout << "Functor" << std::endl; + std::vector names = {"Albert", "Bernd", "Cora", + "Dave", "Emil", "Frank"}; + + auto space_joined_string = + std::accumulate(std::next(names.begin()), names.end(), names.front(), + join_strings_functor()); + + std::cout << "Space-joined string: " << space_joined_string << std::endl; + + auto comma_joined_string = + std::accumulate(std::next(names.begin()), names.end(), names.front(), + join_strings_functor(", ")); + + std::cout << "Comma-joined string: " << comma_joined_string << std::endl; + return EXIT_SUCCESS; }