Moreover, C++ syntax is someway cumbersome and not clean. In some cases (not so much to be worth noting, yet they jumped in my sight) it is misleading; I believe the usage of the same keyword to mean two different things is not good (C too has this flaw, e.g. the keyword static), and if the previous standard had a third meaning for it, the situation could be confusing (I am thinking about auto). Yes, it's not a hokum but it gives the impression of something too layered and hard to handle. Modern languages without a heavy heritage have less or nothing at all of this kind of flaws.
Anyway, let's see an example of folding done with the use of a lambda function.
This file contains 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
#include <iostream> | |
#include <numeric> | |
#include <vector> | |
using namespace std; | |
int main() | |
{ | |
vector<int> array = {1, 2, 3, 4, 5}; | |
int r = accumulate(array.begin(), array.end(), 0, | |
[](const int& x, const int& y) -> int { return x + y*2; }); | |
cout << r << endl; | |
return 0; | |
} |
Not particularly useful, but it shows the equivalent in C++11 of what in other languages we can write. E.g.
Smalltalk
This file contains 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
#(1 2 3 4 5) inject: 0 into: [ :x :y | x + (y*2) ]. |
Common Lisp
This file contains 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
(reduce (lambda (x y) (+ x (* y 2))) '(1 2 3 4 5) :initial-value 0) |
Haskell
This file contains 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
foldl (\x y -> x + y*2) 0 [1, 2, 3, 4, 5] |
Erlang
This file contains 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
lists:foldl(fun(Y, X) -> X + Y*2 end, 0, [1,2,3,4,5]). |
Perl5
This file contains 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
use List::Util qw(reduce); | |
my r = reduce {a + $b * 2 } 0, 1..5; |
Python
This file contains 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
reduce(lambda x, y: x + y*2, [1,2,3,4,5], 0) |
All these snippets run effortlessly and without the need of adding other lines of code; C++ needs "boilerplate" code, though the working line is as simple as
This file contains 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
accumulate(array.begin(), | |
array.end(), 0, | |
[](const int& x, const int& y) -> int { return x + y*2; }) |
(note the use of -> here, which is not syntactic sugar for (*ptr_to_obj). as it is in other contexts).
It could be funny to use some of the new C++11 features; sometimes it could be even useful; but most of the time I believe it won't be a need and C++ is becoming one of the languages with a very large set of (maybe interesting) features which are even the most misunderstood, misused, or simply ignored.