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

refactor: Make Join() util work with any container type #25879

Merged
merged 3 commits into from
Aug 24, 2022

Conversation

maflcko
Copy link
Member

@maflcko maflcko commented Aug 19, 2022

This allows to drop some code

Also remove redundant return type that can be deduced by the compiler.
@NorrinRadd
Copy link

LGTM

Copy link
Contributor

@stickies-v stickies-v left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approach ACK fa75227

@@ -58,34 +58,29 @@ void ReplaceAll(std::string& in_out, const std::string& search, const std::strin
}

/**
* Join a list of items
* Join items
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: perhaps a bit more info:

Suggested change
* Join items
* Join all collection items into an object with the same type as the first item, separated by a separator

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That'd be incorrect. It isn't the type of the first item, but the type returned by the unary op executed on the first item. In practise this will (almost) always be std::string, so it seems odd to be overly verbose?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, no strong feelings about it. For a helper function that's meant to be used all over the codebase I just think it's worth thinking more about a helpful docstring, maybe a join function is not be trivial to everyone? Final suggestion but I don't want to waste time over this so feel free to ignore if you don't think it's helpful:

Suggested change
* Join items
* Join all container items. Typically used to concatenate strings but accepts containers with
* elements of any type.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx, done

Comment on lines 71 to 76
bool first{true};
for (const auto& item : collection) {
if (!first) ret += separator;
ret += unary_op(item);
first = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could potentially do away with first:

Suggested change
bool first{true};
for (const auto& item : collection) {
if (!first) ret += separator;
ret += unary_op(item);
first = false;
for (const auto& item : collection) {
if (&item != &(*collection.begin())) ret += separator;
ret += unary_op(item);
}

Copy link
Member Author

@maflcko maflcko Aug 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That wouldn't compile for all types. For example, https://en.cppreference.com/w/cpp/container/vector_bool/reference has no operator&

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks for the info. Didn't know about this, that's annoying!

*
* @param list The list to join
* @param collection The items to join
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: container seems to be the term used in c++, would it be better to be consistent with that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think I got collection from python https://docs.python.org/3/library/collections.html

template <typename T, typename T2>
T Join(const std::vector<T>& list, const T2& separator)
template <typename C, typename S>
auto Join(const C& list, const S& separator)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: parameter name inconsistent

Suggested change
auto Join(const C& list, const S& separator)
auto Join(const C& collection, const S& separator)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx, done

@naumenkogs
Copy link
Member

utACK fabedbc


btw, do you wanna update PR name and commit messages to use container instead of collection too?

@maflcko maflcko changed the title refactor: Make Join() util work with any collection type refactor: Make Join() util work with any container type Aug 24, 2022
@maflcko
Copy link
Member Author

maflcko commented Aug 24, 2022

thx, renamed title

Copy link
Contributor

@stickies-v stickies-v left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK fa95315

@naumenkogs
Copy link
Member

ACK fa95315

@maflcko maflcko merged commit c89fabf into bitcoin:master Aug 24, 2022
@maflcko maflcko deleted the 2208-join-🎶 branch August 24, 2022 09:32
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Aug 24, 2022
…er type

fa95315 Use new Join() helper for ListBlockFilterTypes() (MacroFake)
fa1c716 Make Join() util work with any container type (MacroFake)
faf8da3 Remove Join() helper only used in tests (MacroFake)

Pull request description:

  This allows to drop some code

ACKs for top commit:
  naumenkogs:
    ACK fa95315
  stickies-v:
    ACK [fa95315](bitcoin@fa95315)

Tree-SHA512: efd65b65722f46b221bd53140ff22bd8e45adc83617980233f28f695be3108a6ab01affd751d715134ffcb9762228ba8952e9467e590cff022c83e0f5404cb74
@bitcoin bitcoin locked and limited conversation to collaborators Aug 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants