Skip to content

Introduce rtl::set<T>, rtl::populator #1

@lorents

Description

@lorents

rtl::set<T> would be an observable set, with operators like

  • union sets, or, |
  • intersect sets, and, &
  • exclusive of sets, xor, ^
  • difference of sets, diff, -
  • mapping elements
  • filtering elements
  • ...

rtl::populator would be a sibling of rtl::animator where each new element is transformed into a collectable resource which is collected when removed.

#include <rtl/set.h>
#include <rtl/populator.h>
...

rtl::set<int> foo = { 0, 1, 2 };
rtl::set<int> bar = { 2, 3, 0 };
rtl::set<int> foobar = foo | bar;

rtl::set<int> baz = foobar
	.filter([](const int& i) { return i > 0; })
	.map<int>([](const int& i) { return i * 2; });
		
rtl::animator printer = { [&]() 
{
	cout << "assert(foobar == { ";
	for (auto it = foobar.begin(); it != foobar.end(); ++it)
	{
		cout << (*it);
		if(it != foobar.last()) cout << ", ";
	}
	cout << " });" << endl;
}};

rtl::populator pop = foobar.populate([&](const string& s)
{
	cout << "// " << s << " added to foobar" << endl;
	return [s]()
	{
		cout << "//" << s << " removed from foobar" << endl;
	};
});

cout << "foo.add(4);" << endl;
foo.add(4);
cout << "foo.remove(0);" << endl;
foo.remove(0);

cout << "bar.add(2);" << endl;
bar.add(2);
cout << "bar.remove(1);" << endl;
bar.remove(1);

There is also no reason not to support modifying sets that are already collections. this will behave like a modifier on the previous value.

foobar.add(7);
foobar.remove(2);

would really be the same as

foobar = foobar | { 7 };
foobar = foobar - { 2 };

Further, rtl::populator would make sense to work with rtl::var<T> too, behaving like a list with a single element

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureSomething new is wanted

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions