The Clever Design and Shortcomings of C++'s std::adjacent_difference
This article delves into the design philosophy of the `std::adjacent_difference` algorithm in the C++ standard library. This algorithm computes the differences between adjacent elements of an input sequence, copying the first element to the output. While this design ensures symmetry with `std::partial_sum`, mirroring differentiation and integration in calculus, it also limits its genericity, as the difference between elements of an arbitrary type might have a different type. The article further draws parallels to derivatives and integrals in calculus, explaining the algorithm's design rationale and contrasting it with Q's more flexible `deltas` function. The conclusion is that, while Stepanov's original intent was sound, the algorithm lacks genericity; C++23's `pairwise_transform` offers a more flexible alternative.
Read more