Andrew Gazelka
02/28/2019, 3:30 PMinterface Problem<T> {
fun solve() : T
}
Davio
02/28/2019, 3:31 PMwbertan
02/28/2019, 3:35 PMUtils
classes? So you can write the test and code with TDD for each Extension in isolation and then move to the other part itself?Andrew Gazelka
02/28/2019, 3:35 PMfun productSumFactors(max: Int) = sequence {
val size = log2(max.toDouble()).toInt() + 1
val array = List(size) { 1 }.toIntArray()
while (true) {
var indexOn = array.size - 1
array[indexOn]++
var reduced: Int
while (array.reduce { acc, i -> acc * i }.also { reduced = it } > max) {
indexOn--
if (indexOn < 0) return@sequence
val nextIndex = array[indexOn]++
array[indexOn + 1] = nextIndex
}
val sum = array.sum()
val k = size + (reduced - sum)
yield(FactorResult(k, reduced))
}
}
?Davio
02/28/2019, 3:36 PM2.isPrime()
Andrew Gazelka
02/28/2019, 3:39 PM2.isPrime()
... however, in this scenario I would only be using this functions for this one problemkarelpeeters
02/28/2019, 3:41 PMDavio
02/28/2019, 3:41 PMSequence<T>.distinctSum()
wbertan
02/28/2019, 3:41 PMsolve
function regardless of what is inside, which makes easy for later refactor the code, move code around, etc, and your test keeps checking if you are not breaking things up.Andrew Gazelka
02/28/2019, 3:46 PMsolve()
is what if I have three separate, specific, coupled functions used in solve()
I might want to test each function individually to better pinpoint what is not working when I am developing itkarelpeeters
02/28/2019, 3:47 PMwbertan
02/28/2019, 3:51 PMsolve
giving 6
returns 30
, etc. You can achieve today that value using the while
as you are doing, and tomorrow refactor that to use an external library, or use a tail recursive function, your test will be the same (the behaviour didn't change).