4 ms·
Cool, but I'm left with some observations: 1) C++ already provides a transform and accumulate (just for starters) which are much like map and fold. Isn't it be
by hellofunk 10y ago
Cool, but I'm left with some observations:
1) C++ already provides a transform and accumulate (just for starters) which are much like map and fold. Isn't it better to learn to use the built-in tools rather than rely on a library? There is quite a lot of functional-style stuff in the standard already.
2) The cost of these abstractions is not clear. You can do a lot of functional idioms in C++, but often a tremendous amount of copying is happening compared to an imperative solution, because C++ lacks functional data structures under the hood.
3) Points 1) and 2) above are largely addressed by proposals for C++20 specification, such as the Range library.
- BeetleB 10y agotransform in C++ is not that great. The standard does not guarantee order is preserved, which makes it virtually useless. People recommend for_each if you want to preserve the order, but it's almost the same as writing a for loop. As for cost, I had a C++ teacher who once sat on the standards committee. His advice: Always use the algorithms library even if it "feels" like it is costlier. Reasons: 1. Much lower likelihood of having a bug. 2. Virtually every time someone decided writing an explicit solution rather than using the algorithms library, it did not perform better (even in cases where it seems "obvious" it should).
- hellofunk 10y ago> The standard does not guarantee order is preserved Are you sure about that? The standard does not guarantee which order transform will actually run and apply the function, but that is a different matter than the order of the final result. I'm pretty sure the standard guarantees that. If you have side effects and require application order guarantees, for_each is the one to use. edit: this SO answer from a Google engineer suggests I am correct: http://stackoverflow.com/questions/34167638/stdtransform-order-guarantees http://stackoverflow.com/questions/34167638/stdtransform-ord...
- BeetleB 10y agoAh, yes, you are correct. Will now happily use transform :-)