Skip to content

Instantly share code, notes, and snippets.

@ppajdek
Created June 24, 2020 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ppajdek/0ab8cfffb2987f1488416cc0f00271cb to your computer and use it in GitHub Desktop.
Save ppajdek/0ab8cfffb2987f1488416cc0f00271cb to your computer and use it in GitHub Desktop.
int imperative() {
int sum = 0;
int limit = 10;
for (int i = 0; i < 100; i++) {
if (i % 3 == 0 && i % 2 == 0) {
if (limit-- == 0) break;
sum += i * i;
}
}
return sum;
}
int declarative() {
return IntStream.range(0, 100)
.filter(i -> i % 3 == 0)
.filter(i -> i % 2 == 0)
.limit(10)
.map(i -> i * i)
.sum();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment