Ładne zestawienie :)
Ale wydaje mi się, że przykład odnośnie ekspresywności języka jest słabo dobrany. Bo tu bardziej zostało pokazane ile dany język ma takiego początkowego boilerplate.
Myślę, że lepszym przykładem byłby np program mający wykonać następujące kroki:
- stwórz listę liczb w kolejności malejącej 19 do 0
- pomiń liczby parzyste
- podnieś każdą liczbę do kwadratu
- posortuj rosnąco
- wypisz wynik
C++
#include
#include
#include
#include
using namespace std;
int main() {
vector<int> s, a;
for (int i = 20; i > 0; i--) s.push_back(i);
copy_if (s.begin(), s.end(), back_inserter(a), [](int x){return x % 2 == 1 ;} );
transform(a.begin(), a.end(), a.begin(), [](int x){return x*x;});
sort(a.begin(), a.end());
for (auto&& i : a) cout << i << " ";
return 0;
}
Wynik:
1 9 25 49 81 121 169 225 289 361
Java
import java.util.Arrays;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
int[] a = IntStream.range(0, 20)
.map(i -> 20 - i - 1)
.filter(x -> x % 2 == 1)
.map(x -> x * x)
.sorted()
.toArray();
System.out.println(Arrays.toString(a));
}
}
Wynik:
[1, 9, 25, 49, 81, 121, 169, 225, 289, 361]
python
a = filter(lambda x: x % 2 == 1, reversed(range(20)))
a = list(map(lambda x: x*x, a))
print(list(sorted(a)))
Wynik:
[1, 9, 25, 49, 81, 121, 169, 225, 289, 361]
Haskell :)
sort $ map (^2) $ filter odd [20, 19..1]
Wynik:
[1,9,25,49,81,121,169,225,289,361]
RE: Krótki opis wad i zalet Pythona