Skip to content

Commit 57ee632

Browse files
Add small improvements to FactorialCalculator.kt
1 parent 203bb38 commit 57ee632

File tree

1 file changed

+17
-19
lines changed
  • app/src/main/java/com/lukaslechner/coroutineusecasesonandroid/usecases/coroutines/usecase12

1 file changed

+17
-19
lines changed

app/src/main/java/com/lukaslechner/coroutineusecasesonandroid/usecases/coroutines/usecase12/FactorialCalculator.kt

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ class FactorialCalculator(
1313
factorialOf: Int,
1414
numberOfCoroutines: Int
1515
): BigInteger {
16-
17-
val subRanges = createSubRangeList(factorialOf, numberOfCoroutines)
1816
return withContext(defaultDispatcher) {
17+
val subRanges = createSubRangeList(factorialOf, numberOfCoroutines)
1918
subRanges.map { subRange ->
2019
async {
2120
calculateFactorialOfSubRange(subRange)
@@ -28,7 +27,7 @@ class FactorialCalculator(
2827
}
2928
}
3029

31-
suspend fun calculateFactorialOfSubRange(
30+
private suspend fun calculateFactorialOfSubRange(
3231
subRange: SubRange
3332
): BigInteger {
3433
return withContext(defaultDispatcher) {
@@ -42,27 +41,26 @@ class FactorialCalculator(
4241
}
4342
}
4443

45-
suspend fun createSubRangeList(
44+
private fun createSubRangeList(
4645
factorialOf: Int,
4746
numberOfSubRanges: Int
48-
): List<SubRange> =
49-
withContext(defaultDispatcher) {
50-
val quotient = factorialOf.div(numberOfSubRanges)
51-
val rangesList = mutableListOf<SubRange>()
47+
): List<SubRange> {
48+
val quotient = factorialOf.div(numberOfSubRanges)
49+
val rangesList = mutableListOf<SubRange>()
5250

53-
var curStartIndex = 1
54-
repeat(numberOfSubRanges - 1) {
55-
rangesList.add(
56-
SubRange(
57-
curStartIndex,
58-
curStartIndex + (quotient - 1)
59-
)
51+
var curStartIndex = 1
52+
repeat(numberOfSubRanges - 1) {
53+
rangesList.add(
54+
SubRange(
55+
curStartIndex,
56+
curStartIndex + (quotient - 1)
6057
)
61-
curStartIndex += quotient
62-
}
63-
rangesList.add(SubRange(curStartIndex, factorialOf))
64-
rangesList
58+
)
59+
curStartIndex += quotient
6560
}
61+
rangesList.add(SubRange(curStartIndex, factorialOf))
62+
return rangesList
63+
}
6664
}
6765

6866
data class SubRange(val start: Int, val end: Int)

0 commit comments

Comments
 (0)