Tuple library
September 26, 2020 ยท View on GitHub
Tuple are now really well implemented and used word wide.
However, writing std::get<0>(myTuple) is annoying.
Tuple with squared brackets
To enable this feature, you must include ltl/Tuple.h header file.
The tuple type is ltl::tuple_t
auto tuple = ltl::tuple_t<int, double, std::string>{};
tuple[0_n] = 8;
tuple[1_n] = 8.0;
tuple[2_n] = "LTL is great"s;
They are some helpers type:
/// Convenience types
template <bool... Bs> using bool_list_t = tuple_t<bool_t<Bs>...>;
template <int... Ns> using number_list_t = tuple_t<number_t<Ns>...>;
template <typename... Types> using type_list_t = tuple_t<type_t<Types>...>;
// Convenience variables
template <bool... Bs> constexpr bool_list_t<Bs...> bool_list_v{};
template <int... Ns> constexpr number_list_t<Ns...> number_list_v{};
template <typename... Types> constexpr type_list_t<Types...> type_list_v{};
Tuple algorithms
Algorithms for tuple are in the ltl/tuple_algos.h header file.
Firstly, tuple are foreach able and apply able.
auto tuple = ltl::tuple_t{10, 18.0}; // CTAD tuple_t<int, double>
auto sum = tuple([](auto ...xs) {return 0 + ... + xs;}; // double(28.0)
for_each(tuple, [](auto x){std::cout << x << "\n";});
They are transformable
auto tuple = ltl::tuple_t{10, 18.0};
auto tuple_string = ltl::transform_type(tuple, lift(std::to_string)); // tuple<string, string>("10", "18");
When you are dealing with a type_list_t, you can use following algorithms:
find_type(tuple, type): Returns anoptional_type<IndexType>find_if_type(tuple, predicate): Returns anoptional_type<IndexType>contains_type(tuple, type): Returnstrue_vorfalse_vif type is contained in tuplecontains_if_type(tuple, predicate): Returnstrue_vorfalse_vif one type of tuple satisfied the predicatecount_type(tuple, type): Returns the number of occurence of type in tuplecount_if_type(tuple, predicate): Returns the number of occurence of types that satisfy the predicateall_of_type(tuple, predicate): Returnstrue_vif all types satisfy the predicateany_of_type(tuple, predicate): Returnstrue_vif at least one type satisfy the predicat enone_of_type(tuple, predicate): Returns!any_of_type(tuple, predicat)unique_type(tuple): Returns atype_list_twithout duplicatesfilter_type(tuple, predicate): Filter the list and remove types that does not satisfy the predicatetransform_type: Apply f to each element in a tuple and builds a tuple from each resultszip_with(f, tuples...): Call f(tuples[0][0], tuples[1][0]...), f(tuples[1][0], tuples[1][1])...zip_type(tuples...): return tuple<tuple<tuples[0][0], tuples[1][0]...>, tuple<tuples[1][0], tuples[1][1]...>>enumerate_type(tuple): return tuple<tuple<number_t<0>, tuple[0]>, tuple<number_t<1>, tuple[1]>>scanl(f, init, tuple): compute the partial sum of tuple by applying f