[Boost.Hana] how to merge two or more maps
Hi, I am looking into the documentation of hana to merge two or more maps, say: #include <boost/hana.hpp> namespace hana = boost::hana; int main(int argc, char **argv) { constexpr auto m1 = hana::make_map( hana::make_pair("key1"_s, hana::type_c<std::string>), hana::make_pair("key2"_s, hana::type_c<std::string>) ); constexpr auto m2 = hana::make_map( hana::make_pair("key3"_s, hana::type_c<std::string>), hana::make_pair("key4"_s, hana::type_c<std::string>), hana::make_pair("key5"_s, hana::type_c<std::string>) ); // Merge two or more maps? constexpr auto result = m1 + m2; return 0; } I'm not sure how to do this, probably taking the first map and for every pair in the subsequent map return an insert? In pseudo code: constexpr result = hana::insert(hana::insert(m1, pair1), pair2); Can someone give me some pointers in the right direction? Regards, Matthijs
Hana does not provide a way to merge two maps out of the box, although that would be very useful. I added a feature request at [1]. For now, you can use `fold_left` with `insert` to insert each element of the #include <boost/hana.hpp> #include <string> namespace hana = boost::hana; using namespace hana::literals; constexpr auto m1 = hana::make_map( hana::make_pair("key1"_s, hana::type_c<std::string>), hana::make_pair("key2"_s, hana::type_c<std::string>) ); constexpr auto m2 = hana::make_map( hana::make_pair("key3"_s, hana::type_c<std::string>), hana::make_pair("key4"_s, hana::type_c<std::string>), hana::make_pair("key5"_s, hana::type_c<std::string>) ); constexpr auto result = hana::fold_left(m1, m2, hana::insert); constexpr auto expected = hana::make_map( hana::make_pair("key1"_s, hana::type_c<std::string>), hana::make_pair("key2"_s, hana::type_c<std::string>), hana::make_pair("key3"_s, hana::type_c<std::string>), hana::make_pair("key4"_s, hana::type_c<std::string>), hana::make_pair("key5"_s, hana::type_c<std::string>) ); static_assert(result == expected, ""); int main() { } Also, while posting on this list is OK, please note that the preferred way to ask Hana-related questions is on StackOverflow with the [boost-hana] tag, as documented at [2]. This allows everybody to benefit from questions and answers, and I am registered to the tag so there's no chance that I miss your question as on this list. Regards, Louis [1]: https://github.com/boostorg/hana/issues/299 [2]: http://boostorg.github.io/hana/#tutorial-support -- View this message in context: http://boost.2283326.n4.nabble.com/Boost-Hana-how-to-merge-two-or-more-maps-... Sent from the Boost - Users mailing list archive at Nabble.com.
Thank you for your answer. Regards, Matthijs On 8/25/16 3:06 AM, Louis Dionne wrote:
Hana does not provide a way to merge two maps out of the box, although that would be very useful. I added a feature request at [1]. For now, you can use `fold_left` with `insert` to insert each element of the
#include <boost/hana.hpp> #include <string> namespace hana = boost::hana; using namespace hana::literals;
constexpr auto m1 = hana::make_map( hana::make_pair("key1"_s, hana::type_c<std::string>), hana::make_pair("key2"_s, hana::type_c<std::string>) );
constexpr auto m2 = hana::make_map( hana::make_pair("key3"_s, hana::type_c<std::string>), hana::make_pair("key4"_s, hana::type_c<std::string>), hana::make_pair("key5"_s, hana::type_c<std::string>) );
constexpr auto result = hana::fold_left(m1, m2, hana::insert); constexpr auto expected = hana::make_map( hana::make_pair("key1"_s, hana::type_c<std::string>), hana::make_pair("key2"_s, hana::type_c<std::string>), hana::make_pair("key3"_s, hana::type_c<std::string>), hana::make_pair("key4"_s, hana::type_c<std::string>), hana::make_pair("key5"_s, hana::type_c<std::string>) );
static_assert(result == expected, "");
int main() { }
Also, while posting on this list is OK, please note that the preferred way to ask Hana-related questions is on StackOverflow with the [boost-hana] tag, as documented at [2]. This allows everybody to benefit from questions and answers, and I am registered to the tag so there's no chance that I miss your question as on this list.
Regards, Louis
[1]: https://github.com/boostorg/hana/issues/299 [2]: http://boostorg.github.io/hana/#tutorial-support
-- View this message in context: http://boost.2283326.n4.nabble.com/Boost-Hana-how-to-merge-two-or-more-maps-... Sent from the Boost - Users mailing list archive at Nabble.com. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Louis Dionne
-
Matthijs Möhlmann