transform_reduce to fix count implementation#7
Open
vug wants to merge 1 commit intocodereport:mainfrom
Open
Conversation
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}` with `init` is: `R(R(R(a, b), R(c, d)), init)`, NOT `R(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 of `reduce` part of `transform_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` and `transform_reduce` versions: https://godbolt.org/z/r66M4cYsb Have a nice day!
Author
|
This might break the flow of your presentation though. :-( Your favorite algorithm |
Author
|
By the way, one other thing I've tried before switching to |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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}withinitis: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 ofreducepart oftransform_reducecommutative 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
reduceandtransform_reduceversions: https://godbolt.org/z/r66M4cYsbHave a nice day!