|
|
|
|
|
by stickfigure
1611 days ago
|
|
Here's a Java example that sums the populations of a list of Countries: int population = countries.stream().mapToInt(Country::getPopulation).sum();
The Go implementation: var population = 0
for _, country := range countries {
population += country.Population
}
It gets more perverse if you need to flatMap, or transmute components of map types, etc. If you want even more power, take a look at https://github.com/amaembo/streamex. This sort of container manipulation is bread and butter for business processing. I use it every day, sometimes with a dozen operations. This (with liberal use of `final` values) makes for some pretty functional-looking code.I'll grant you the Kotlin or Scala version is slightly more compact. But not fundamentally different, like the Go version. I (and the pretty much every language designer in the post-Java era) disagree with you about checked exceptions, but that's a whole different thread... |
|
Something else to consider is performance, in most implementations the for loop is going to be more efficient.