These are chat archives for boostorg/hana

3rd
Jul 2017
Barrett Adair
@badair
Jul 03 2017 03:38

Is monadic_fold_left the best way to flatten something like this (an edge list)...

hana::tuple<
    hana::pair<A, B>,
    hana::pair<A, C>,
    hana::pair<A, D>,
    hana::pair<B, C>,
    hana::pair<B, D>>

...into something like this (an adjacency list)?

hana::tuple<
    hana::pair<A, hana::tuple<B, C, D>>,
    hana::pair<B, hana::tuple<C, D>>>
Jason Rice
@ricejasonf
Jul 03 2017 03:42
like a "group_by"?
Barrett Adair
@badair
Jul 03 2017 03:43
exactly
Jason Rice
@ricejasonf
Jul 03 2017 03:44
do you have a working example?
Barrett Adair
@badair
Jul 03 2017 03:45
No. I have my own implementation of group_by but it sucks
Jason Rice
@ricejasonf
Jul 03 2017 03:45
if there is only A and B then partition would be really fast
Barrett Adair
@badair
Jul 03 2017 03:48
There are more than A and B. Does hana have a group by operation? If not, is there interest in a PR?
Jason Rice
@ricejasonf
Jul 03 2017 03:48
boostorg/hana#324
I have a couple of use cases for one
Having it build a map would be ideal (I think)
perhaps ignore that code I posted in there :P
Barrett Adair
@badair
Jul 03 2017 03:52
I agree. I see that there is "group" in hana, but it looks like it excludes the predicate result from the group result
Jason Rice
@ricejasonf
Jul 03 2017 04:08
I'm certain a PR for that would be welcome.
Barrett Adair
@badair
Jul 03 2017 04:57
Clang 4 and Clang 5 give weird constexpr errors here (compiler bug?), and GCC 7 doesn't yield the result I would expect. Trying to group by first type in a pair -- is my predicate wrong? I think so, because the ordering of the input tuple elements affects the output type by more than just the output order. What's the best way to do this?
#include <utility>
#include <type_traits>
#include <boost/hana.hpp>

namespace hana = boost::hana;

struct A{};
struct B{};
struct C{};
struct D{};

int main() {

    auto grouped = hana::group(
        hana::make_tuple(
            std::pair<A, B>{},
            std::pair<A, C>{},
            std::pair<B, C>{},
            std::pair<A, D>{},
            std::pair<B, D>{}),
        hana::comparing([](auto t){ return hana::type_c<decltype(t.first)>; }));

    // print type as error message
    decltype(grouped)::asdfasdfasdf();
}