Skip to content

Commit a8b422e

Browse files
committed
#121 add answer in 5-0-2
1 parent 5d05b43 commit a8b422e

File tree

1 file changed

+3
-6
lines changed
  • 5-0-functional-programming/5-0-2-stream-sum-of-squares/src/main/java/com/bobocode/fp

1 file changed

+3
-6
lines changed

5-0-functional-programming/5-0-2-stream-sum-of-squares/src/main/java/com/bobocode/fp/SumOfSquares.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,8 @@ static int calculateSumOfSquaresInRange(int startInclusive, int endInclusive) {
2727
throw new InvalidRangeException();
2828
}
2929

30-
// todo: refactor using functional approach – instead of using for loop, use IntStream.rangeClose()
31-
int sumOfSquares = 0;
32-
for (int i = startInclusive; i <= endInclusive; i++) {
33-
sumOfSquares += i * i;
34-
}
35-
return sumOfSquares;
30+
return IntStream.rangeClosed(startInclusive, endInclusive)
31+
.map(a -> a * a)
32+
.sum();
3633
}
3734
}

0 commit comments

Comments
 (0)