From 57a9fcc9bc7c9d1e34c80b9bd57f79bd42a88872 Mon Sep 17 00:00:00 2001 From: Sandipan Mohanty <s.mohanty@fz-juelich.de> Date: Sun, 12 May 2024 16:11:21 +0200 Subject: [PATCH] Move a few examples in and out of chapter 1 --- chapter_01/examples/consteval.cc | 23 +++++++++++++++++++ chapter_01/examples/iotaloop.cc | 12 ++++++++++ chapter_01/examples/iotaloop2.cc | 16 +++++++++++++ chapter_01/solutions/iotaloop3.cc | 16 +++++++++++++ .../examples/numsort.cc | 0 .../examples/sharedptr.cc | 0 .../examples/uniqueptr.cc | 0 7 files changed, 67 insertions(+) create mode 100644 chapter_01/examples/consteval.cc create mode 100644 chapter_01/examples/iotaloop.cc create mode 100644 chapter_01/examples/iotaloop2.cc create mode 100644 chapter_01/solutions/iotaloop3.cc rename {chapter_01 => chapter_06}/examples/numsort.cc (100%) rename {chapter_01 => chapter_06}/examples/sharedptr.cc (100%) rename {chapter_01 => chapter_06}/examples/uniqueptr.cc (100%) diff --git a/chapter_01/examples/consteval.cc b/chapter_01/examples/consteval.cc new file mode 100644 index 0000000..d473db7 --- /dev/null +++ b/chapter_01/examples/consteval.cc @@ -0,0 +1,23 @@ +#include <algorithm> +#include <ranges> +namespace sr = std::ranges; +namespace sv = sr::views; + +consteval auto is_prime(unsigned N) -> bool +{ + return sr::none_of( + sv::iota(2U) + | sv::take_while([=](auto i) { return i * i <= N; }), + [=](auto i){ return N % i == 0; }); +} + +auto main() -> int +{ + return (sr::fold_left( + sv::iota(2U) + | sv::filter([](unsigned i) { return is_prime(i); }) + | sv::take(10U), + 0UL, + std::plus<unsigned long>{})); +} + diff --git a/chapter_01/examples/iotaloop.cc b/chapter_01/examples/iotaloop.cc new file mode 100644 index 0000000..b3535e3 --- /dev/null +++ b/chapter_01/examples/iotaloop.cc @@ -0,0 +1,12 @@ +#include <print> +#include <ranges> + +namespace sr = std::ranges; +namespace sv = sr::views; + +auto main() -> int +{ + for (auto i : sv::iota(10, 15)) std::print("{}\n", i); +} + + diff --git a/chapter_01/examples/iotaloop2.cc b/chapter_01/examples/iotaloop2.cc new file mode 100644 index 0000000..4545a87 --- /dev/null +++ b/chapter_01/examples/iotaloop2.cc @@ -0,0 +1,16 @@ +#include <print> +#include <ranges> + +namespace sr = std::ranges; +namespace sv = sr::views; +auto divisible_by_4(int i) -> bool +{ + return i % 4 == 0; +} +auto main() -> int +{ + for (auto i : sv::iota(1, 500) | sv::filter(divisible_by_4)) + std::print("{}\n", i); +} + + diff --git a/chapter_01/solutions/iotaloop3.cc b/chapter_01/solutions/iotaloop3.cc new file mode 100644 index 0000000..8452723 --- /dev/null +++ b/chapter_01/solutions/iotaloop3.cc @@ -0,0 +1,16 @@ +#include <print> +#include <ranges> + +namespace sr = std::ranges; +namespace sv = sr::views; +auto my_selection(int i) -> bool +{ + return (i - 10) % 7 == 0; +} +auto main() -> int +{ + for (auto i : sv::iota(-50, 50) | sv::filter(my_selection)) + std::print("{}\n", i); +} + + diff --git a/chapter_01/examples/numsort.cc b/chapter_06/examples/numsort.cc similarity index 100% rename from chapter_01/examples/numsort.cc rename to chapter_06/examples/numsort.cc diff --git a/chapter_01/examples/sharedptr.cc b/chapter_06/examples/sharedptr.cc similarity index 100% rename from chapter_01/examples/sharedptr.cc rename to chapter_06/examples/sharedptr.cc diff --git a/chapter_01/examples/uniqueptr.cc b/chapter_06/examples/uniqueptr.cc similarity index 100% rename from chapter_01/examples/uniqueptr.cc rename to chapter_06/examples/uniqueptr.cc -- GitLab