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

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
User avatar
chili
Site Admin
Posts: 3894
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

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

Post by chili » January 22nd, 2020, 9:01 am

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
Chili

albinopapa
Posts: 3982
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

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

Post by albinopapa » January 22nd, 2020, 6:16 pm

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.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3894
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

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

Post by chili » January 23rd, 2020, 6:02 am

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.
Chili

albinopapa
Posts: 3982
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

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

Post by albinopapa » January 23rd, 2020, 9:29 am

I have not.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply