tuple操作ライブラリ「tupleple」

ギッハッハブ
Fuyutsubaki/tupleple · GitHub

タプルプルプルタプルプル
ご意見もらえると喜びます

できること一覧

  • at

N番目の要素にアクセスする

  • apply

タプルに関数を適用する

view

おおむね元要素へのアクセスのインデックスを架け替えてるだけである
基本的に遅延評価で、要素にアクセスできて、ムーブできる


  • cat

繋げる

using namespace tupleple;
auto a = std::make_tuple(1, 2);
auto b = std::make_tuple(std::make_unique<int>(42), 'B');
auto c = std::make_tuple(std::string(":-)"), std::string(":-|"));
auto p = view::cat(a, std::move(b), c) | at<2>();//unique_ptr<int>(42)
  • take / drop

先頭N個を拾ったり落としたりする

auto tuple = std::make_tuple(1, std::make_unique<int>(2), 3);
auto x = std::move(tuple) | view::drop<1>();
auto i = at<0>(std::move(x));//unique_ptr<int>(2)
  • filter

条件に合うものだけを残す

auto r = std::make_tuple(false, "ABC", 3.14, 42);
auto c = r | view::filter_if<std::is_integral>();
std::cout << (c | at<1>());
  • map

各要素に関数を適用する

auto r = std::make_tuple(3.5, 1, 3.14, 1.414);
auto c = r | view::map([](double d){return d * 2; });
auto d = c | at<2>();//6.28
  • reverse

順序を逆転させる

auto x = std::make_tuple(1, 2, 3, 4) | view::reverse() | at<0>();
  • zip

複数のタプルをくっつける

auto t1 = std::make_tuple(1, 2, 3);
auto t2 = std::make_tuple('A', 'B', 'C');
auto t3 = std::make_tuple(std::make_unique<int>(42), true, false);

auto result = tupleple::view::zip(t1, t2, std::move(t3));
auto c1 = std::move(result) | at<0>();
auto r = std::move(c1) | at<2>();	//unique_ptr 42

algorithm

  • binary_fold

二分木畳み込み。右でも左でもない

struct plus
{
	template<class L,class R>
	auto operator()(L l,R r)const
	->decltype(l+r)
	{
	return l+r;
	}
};

int main()
{
	using namespace tupleple;
	auto t = std::make_tuple(1, 1.4f, 31.4, 18L);
	auto x = algorithm::binary_fold(t, plus());
}
  • for_each

全要素をなめる。順序が乱れない

type_list

タイプリスト処理。タプルとタイプリストは切っても切り離せない

utility

いろいろ