I really like that modern c++ is getting these more functional features but tbh i think they aren't what i'd use here. I Think it's great when some raw loop can be replaced by some more readable desciptive algorithm, even better if it leads to shorter code. But do people really think
Honestly, it's not. I really pushed it and this solution is actually undefined behaviour since the lambdas are not regular invocable.
But I do think that from a software engineering perspective, moving to ugly functional code will be a good move. Simply because it makes static analysis so much more powerful.
I wish we could write beautiful functional code in C++, but I don't think that will ever happen :-D
It's an unreadable mess. I do not for the life of me understand why people have an issue with seeing a for or while loop. They think it is bad code for some reason which is just bizarre to me. It's far easier to read and shows you *what is actually happening*. It is much better than code compressed into chained, filthy functional bollocks.
I think I have seen more abuses of std::accumulate as a clever trick to avoid writing a loop, using side effects to achieve the desired goal, than to actually compute a sum.
That's not surprising at all – accumulate isn't a poorly-named generalized summation, it's a poorly-named left fold. That said, anyone putting side-effects in their algorithm operations is sorely missing the point and needs to be swatted over the nose with a newspaper a few times.
I don't know who told you that but the name of a function tells you what someone thinks is happening while the actual code tells you what is happening.
Function names guide you, code tells you what's actually going on.
If you write a for loop and breaking when an if statement is true, you have written a find_if. Isn't it simpler to just write find_if and instead of writing a for loop explaining how you find something?
Why is writing the loop the more complicated option here? It's not. It shows exactly what is happening.
find_if has ambiguity. It's also not very modifiable. What if I need to add extra logic per iteration?
The functional version is effectively a form of compression which is why it's not good for readability. I need to uncompress it to fully understand what is happening.
When it comes to complicated logic in an algorithm it's really important that its simple and easily followable. Clumping all of it on one line and hiding behind functions that encapsulate a minutae of logic is bad.
Well said. It is very easy and fast to read simple code, it is not so simple parsing huge chained expressions with lambdas and whatnot. It also is faster to compile, likely much faster to execute, obvious to people less familiar with language, but maybe it doesn't make the author feel as smart.
24
u/wheypoint Ö Dec 15 '21
Great to see c++20 in action and nice write ups
I really like that modern c++ is getting these more functional features but tbh i think they aren't what i'd use here. I Think it's great when some raw loop can be replaced by some more readable desciptive algorithm, even better if it leads to shorter code. But do people really think
is more readable than the equivalent super simple index or iterator based for loop?
Now maybe in haskell you could solve this using a nice functional composition of simple functions but i think it's too much for c++ ranges.