Converting Python code to Kotlin

Solution 1:

I read some documentation (as was suggested in the comments) and this is what I came up with (just in case someone will find that useful)

class DistinctColorsGenerator {
    fun generate(number: Int): List<ColorRgb> = generateRgbs.take(number).toList()

    private val genHsvs = buildSequence {
        fractions.forEach { createHsvColors(it).forEach { yield(it) } }
    }

    private val generateRgbs = buildSequence {
        genHsvs.forEach { yield(it.toRgb()) }
    }

    private val zenosDichotomy = buildSequence {
        var d = 1
        while (true) {
            yield(Fraction(1, d))
            d *= 2
        }
    }

    private val fractions = buildSequence {
        yield(Fraction(0, 1))
        zenosDichotomy.forEach {
            val i = it.denominator.toInt()
            for (j in 1 until i step 2) {
                yield(Fraction(j, i))
            }
        }
    }

    private fun createHsvColors(h: Fraction<Int>) = buildSequence {
        listOf(Fraction(6, 10)).forEach { s ->
            listOf(Fraction(8, 10), Fraction(5, 10)).forEach { v ->
                yield(ColorHsv(h, s, v))
            }
        }
    }
}

Classes ColorRgb, ColorHsv, Fraction are just data classes. ColorHsv.toRgb() I took from here