transform_reduce to fix count implementation #7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi! while watching your talk on YouTube ran this logic on a small example and it didn't give me the correct result.
https://en.cppreference.com/w/cpp/algorithm/reduce says the binary operation has to be associative and commutative. I think
a + (b == val)
is not. Because the way reduce goes over a vector{a, b, c, d}
withinit
is:R(R(R(a, b), R(c, d)), init)
, NOTR(R(R(R(init, a), b), c), d)
. :-O I realized this when I saw the diagram for reduce in https://blog.tartanllama.xyz/accumulate-vs-reduce/.I watched further into your talk and learned about
transform_reduce
. ^_^ Using that we can make the binary op ofreduce
part oftransform_reduce
commutative and associative.Also I'm not an export. The way reduce goes over a vector could be compiler, platform dependent. Here is a Compiler Explorer comparison of
reduce
andtransform_reduce
versions: https://godbolt.org/z/r66M4cYsbHave a nice day!