Page 1 of 1

C++20 Ranges Makes My Wiener Feel Funny and I Like It

Posted: January 22nd, 2020, 9:01 am
by chili

Code: Select all

#include <iostream>
#include <vector>
#include <range/v3/all.hpp>
#include <algorithm>
#include <iterator>
#include <functional>
#include <memory>

struct Foo
{
	Foo(int a, int b) : a(a), b(b) {}
	int a;
	int b;
};

int main()
{
	namespace rn = ranges;
	namespace vi = ranges::views;

	auto v1 = vi::ints | vi::take(10)
		| vi::transform([](int n) {return std::make_unique<Foo>(n * 2, n * 5); })
		| rn::to_vector;
	auto v2 = vi::ints | vi::take(14)
		| vi::transform([](int n) {return std::make_unique<Foo>(n * 6, n * 9); })
		| rn::to_vector;

	auto odd = [](int n) {return n % 2; };
	auto processing = vi::indirect | vi::transform(std::mem_fn(&Foo::b)) | vi::take_last(6) | vi::filter(odd);
	auto range1 = v1 | processing;
	auto range2 = v2 | processing;
	
	for( auto [n1,n2] : vi::zip(range1, range2) | vi::cycle | vi::take(69) )
	{
		std::cout << n1 << ":" << n2 << ' ';
	}

	return 0;
}
https://wandbox.org/permlink/xUvg3WTSZJEj8d9s

Re: C++20 Ranges Makes My Wiener Feel Funny and I Like It

Posted: January 22nd, 2020, 6:16 pm
by albinopapa
Not sure I like the "pipes", but I've read because of the order of operations defined for the C++ language, operator>> and operator<< wouldn't be usable in the same manner...disappointed.

Re: C++20 Ranges Makes My Wiener Feel Funny and I Like It

Posted: January 23rd, 2020, 6:02 am
by chili
To each their own I suppose, but pipes are a very natural choice if you've spent any time with a linux/unix shell cli environment.

Re: C++20 Ranges Makes My Wiener Feel Funny and I Like It

Posted: January 23rd, 2020, 9:29 am
by albinopapa
I have not.