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

Extend the interface of join to pass nested separator down #4240

Closed
correaa opened this issue Nov 16, 2024 · 3 comments
Closed

Extend the interface of join to pass nested separator down #4240

correaa opened this issue Nov 16, 2024 · 3 comments
Labels

Comments

@correaa
Copy link

correaa commented Nov 16, 2024

With fmt, one can pass a range and a separator to fmt::join.
I wonder if it is possible to extend the interface of join (or something with another name) to pass extra parameters that will be utilized internally, if other calls to std::join are made recursively.

So if a nested vector is passed: std::vector<std::vector<T>> is passed to fmt::join( vv, ",", "~") then the top level is separated by "," and the lower level is separated by "~".

(It is possible that it could be a hard error if join is not called internally, or alternative, that the subsequent argument is ignored if not used --more elegant--)

This will replace something like:

...
    constexpr auto join_sp = [](auto&& row) { return fmt::join(row, "~"); };
    fmt::print(
        "{}",
        fmt::join(
            std::views::transform(arr, join_sp),
            ", "
        )
    );

with this: fmt::print( "{}", fmt::join(arr, ", ", "~") ).

https://godbolt.org/z/nKcas4Ka4

See here:
https://stackoverflow.com/questions/79193937/control-fmt-formatting-of-nested-containers-ranges/79194118#79194118

PS: I know this has a flaw that is that the enclosing opening and closing format is still not specified, that needs more though I guess.

Feel free to close this issue if you think that is out of the scope of the library.

@vitaut
Copy link
Contributor

vitaut commented Nov 19, 2024

It's an interesting idea but it seems a bit niche. I think it belongs to a custom formatter rather than the {fmt} library.

@correaa
Copy link
Author

correaa commented Nov 19, 2024

Thank you for considering the idea. Now that there are so many "rank increasing" views, such as std::view::split, std::lazy_split, std::view::slide, std::views::chunk, std::views::chunk_by, it could be a nice way to print those out-of-the-box.

Consider, for example, printing std::vector{1, 2, 3, 4} | stdv::chunk(2).

@vitaut
Copy link
Contributor

vitaut commented Nov 21, 2024

I think the default formatting is sufficient and if anything more fancy is desired it can be done through an adapter.

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

No branches or pull requests

2 participants