Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zip algorithm #53

Open
Quincunx271 opened this issue Jul 6, 2018 · 5 comments
Open

zip algorithm #53

Quincunx271 opened this issue Jul 6, 2018 · 5 comments
Assignees

Comments

@Quincunx271
Copy link
Contributor

Quincunx271 commented Jul 6, 2018

A zip algorithm would be really useful, and I've desired it on occasion. It would perform this operation:

call<
    zip<listify>,
    list<A, B, C>,
    list<int, double, float>
>;
// produces:
list<
    list<A, int>,
    list<B, double>,
    list<C, float>
>;

Here's a sample implementation to show what I'm talking about (godbolt link):

using zip_first = transform<unpack<front<>>>;

struct zip_rest;

template <typename C = listify>
using zip = if_<
    // any<>: if the lengths are different, act like they're all the smallest size
    // front<> could work too; it would act as if they're all the same size as the first list
    any<unpack<size<same_as<uint_<1>>>>>,
    zip_first,
    fork<
        zip_first,
        zip_rest,
        C
    >
>;

struct zip_rest {
    template <typename... Ts>
    using f = call<
        transform<
            unpack<pop_front<>>,
            zip<>
        >,
        Ts...
    >;
};
@chieltbest
Copy link
Contributor

Zip is currently provided in algorithm/zip_with.hpp, although I'd say that it should just be called zip, so I'll get on that.

@chieltbest chieltbest self-assigned this Jul 10, 2018
@Quincunx271
Copy link
Contributor Author

Quincunx271 commented Jul 10, 2018

So that's what that's supposed to do. I struggled to understand the code, and "n-ary version of transform" in the documentation didn't make sense to me.

Regardless, it might also be useful to have a version of zip that works for mismatched lengths. The sample implementation I gave here zips up to the smallest length, and it may also make sense to have a version that zips up to the largest length, using nothing for sequences that ended early. Unsure if it's worthwhile, but could potentially be useful.

@odinthenerd
Copy link
Member

interesting idea, would be quite implementable but I wouldn't know what to call the two mismatched flavors.

zip_jagged?

zip_filled?

shortening to the shortest is a haskell thing right?

@Quincunx271
Copy link
Contributor Author

Maybe it started with haskell, but a lot of other languages do it that way. I've personally never seen a zip which zips to the largest length, but that's actually the way Wikipedia defines it with the other ways being noted as variations.

Maybe zip_shortest for the short version (that's what Boost Hana does)? zip_longest might be a reasonable name for the alternative, for symmetry with zip_shortest.

@odinthenerd
Copy link
Member

zip_long, zip_short and zip? the exact version is going to be the most performant and probably the most commonly used (I think)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants