Skip to content
Snippets Groups Projects
Commit 57a9fcc9 authored by Sandipan Mohanty's avatar Sandipan Mohanty
Browse files

Move a few examples in and out of chapter 1

parent 03e9f377
Branches
No related tags found
No related merge requests found
#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>{}));
}
#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);
}
#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);
}
#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);
}
File moved
File moved
File moved
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment