3 ms·
Really the “problem” here is that C++’s functional constructs will never allocate memory, giving them somewhat strange signatures that don’t really conduce them
by saagarjha 7y ago
Really the “problem” here is that C++’s functional constructs will never allocate memory, giving them somewhat strange signatures that don’t really conduce themselves well to typical functional concepts. At compile time there is no such goal and as such some of these constructs can only be found there.
- MauranKilom 7y agoAccording to https://en.cppreference.com/w/cpp/algorithm/stable_sort https://en.cppreference.com/w/cpp/algorithm/stable_sort: > This function attempts to allocate a temporary buffer equal in size to the sequence to be sorted. If the allocation fails, the less efficient algorithm is chosen. Or is this not what you consider a "functional construct"?
- elbows 7y agoIt's more of an issue with things like std::transform or std::copy_if. Ergonomically, it's nice if these kinds of functions either allocate and return a new container, or return an iterator that yields the elements of the result. But the C++ versions of these take the result location as an argument. It makes chaining them together a hassle because you have to create all the intermediate containers explicitly. I think there are good reasons for the STL to work this way, but it can make programming in a functional style pretty inconvenient compared to a lot of other languages.
- leni536 7y agoRanges will help with some of the composability issues of standard library algorithms. [1] https://en.cppreference.com/w/cpp/ranges https://en.cppreference.com/w/cpp/ranges
- entha_saava 7y agoI know some basic C++ and STL. Is there anything like streams in C++. I mean things would be as easy as vec.stream().map(lambda).fold(T::operator+), so it wouldn't require any more allocations than those done by copy constructors.
- MauranKilom 7y agoThe upcoming ranges library will provide exactly this! It uses | for composing operations. See e.g. here: https://github.com/ericniebler/range-v3/tree/master/example https://github.com/ericniebler/range-v3/tree/master/example