From 87d2db06c121c4fb0e0ca2447c23bd52c3a85cf8 Mon Sep 17 00:00:00 2001 From: szdytom Date: Tue, 18 Jul 2023 20:31:35 +0800 Subject: [PATCH] add outputln variant --- libvmake/examples/.hello_world.cpp.swo | 0 libvmake/examples/filtering.cpp | 7 ++-- libvmake/examples/hello_world.cpp | 2 +- libvmake/examples/random_numbers.cpp | 5 ++- libvmake/vmake.hpp | 50 +++++++++++++++++++++----- 5 files changed, 48 insertions(+), 16 deletions(-) create mode 100644 libvmake/examples/.hello_world.cpp.swo diff --git a/libvmake/examples/.hello_world.cpp.swo b/libvmake/examples/.hello_world.cpp.swo new file mode 100644 index 0000000..e69de29 diff --git a/libvmake/examples/filtering.cpp b/libvmake/examples/filtering.cpp index 3aca603..7ece4c6 100644 --- a/libvmake/examples/filtering.cpp +++ b/libvmake/examples/filtering.cpp @@ -3,11 +3,10 @@ using namespace std; int main() { - vmake::output(cout, " ", vmake::filter(vmake::range(1, 10), [](int x) { + vmake::outputln(cout, " ", vmake::filter(vmake::range(1, 10), [](int x) { return x % 2 == 0; - })); - cout << endl; - vmake::output(cout, " ", vmake::take(vmake::filter(vmake::iota(1), [](int x) { + }), '\n'); + vmake::outputln(cout, " ", vmake::take(vmake::filter(vmake::iota(1), [](int x) { return x <= 5; }), 5)); return 0; diff --git a/libvmake/examples/hello_world.cpp b/libvmake/examples/hello_world.cpp index df1ad28..2d3bc27 100644 --- a/libvmake/examples/hello_world.cpp +++ b/libvmake/examples/hello_world.cpp @@ -3,7 +3,7 @@ using namespace std; int main() { vector a{"HELLO", "WORLD", "!"}; - vmake::output(cout, " ", vmake::transform(vmake::extract(a.begin(), a.end()), [](auto &&s) { + vmake::outputln(cout, " ", vmake::transform(vmake::extract(a.begin(), a.end()), [](auto &&s) { std::transform(s.cbegin(), s.cend(), s.begin(), [] (auto c) { return std::tolower(c); }); return s; })); diff --git a/libvmake/examples/random_numbers.cpp b/libvmake/examples/random_numbers.cpp index a37a3f6..f3d6237 100644 --- a/libvmake/examples/random_numbers.cpp +++ b/libvmake/examples/random_numbers.cpp @@ -3,8 +3,7 @@ using namespace std; int main() { - vmake::output(cout, " ", vmake::take(vmake::rng::uniform_ints(1, 10), 15)); - cout << endl; - vmake::output(cout, " ", vmake::take(vmake::rng::uniform_reals(1., 10.), 5)); + vmake::outputln(cout, " ", vmake::take(vmake::rng::uniform_ints(1, 10), 15)); + vmake::outputln(cout, " ", vmake::take(vmake::rng::uniform_reals(1., 10.), 5)); return 0; } diff --git a/libvmake/vmake.hpp b/libvmake/vmake.hpp index c7a6a2a..2ffe773 100644 --- a/libvmake/vmake.hpp +++ b/libvmake/vmake.hpp @@ -436,26 +436,60 @@ inline OutputIt copy_n(OutputIt it, size_t n, Gen&& g) { return it; } -template -inline auto output(OutputStream& out, const char *delim, Gen &&g) { +template +inline auto output(std::basic_ostream& out, const char *delim, Gen &&g) { return copy(std::ostream_iterator::type::result>(out, delim), std::move(g)); } -template -inline auto output(OutputStream& out, Gen &&g) { +template +inline auto output(std::basic_ostream& out, Gen &&g) { return copy(std::ostream_iterator::type::result>(out), std::move(g)); } -template -inline auto output_n(OutputStream& out, size_t n, const char *delim, Gen &&g) { +template +inline auto output_n(std::basic_ostream& out, size_t n, const char *delim, Gen &&g) { return copy_n(std::ostream_iterator::type::result>(out, delim), n, std::move(g)); } -template -inline auto output_n(OutputStream& out, size_t n, Gen &&g) { +template +inline auto output_n(std::basic_ostream& out, size_t n, Gen &&g) { return copy_n(std::ostream_iterator::type::result>(out), n, std::move(g)); } +template)> +inline auto outputln(std::basic_ostream& out, const char *delim, Gen &&g, const Endl &endl = std::endl) { + auto res = copy(std::ostream_iterator::type::result>(out, delim), std::move(g)); + out << endl; + return res; +} + +template)> +inline auto outputln(std::basic_ostream& out, Gen &&g, const Endl &endl = std::endl) { + auto res = copy(std::ostream_iterator::type::result>(out), std::move(g)); + out << endl; + return res; +} + +template)> +inline auto outputln_n(std::basic_ostream& out, size_t n + , const char *delim, Gen &&g, const Endl &endl = std::endl) { + auto res = copy_n(std::ostream_iterator::type::result>(out, delim), n, std::move(g)); + out << endl; + return res; +} + +template)> +inline auto outputln_n(std::basic_ostream& out, size_t n, Gen &&g, const Endl &endl = std::endl) { + auto res = copy_n(std::ostream_iterator::type::result>(out), n, std::move(g)); + out << endl; + return res; +} + +template)> +inline void outputln(std::basic_ostream& out, const Endl &endl = std::endl) { + out << endl; +} + } #endif