5 small things you probably don’t know about Kotlin
Piotr Ślesarew

Hi — thanks for the inspiring article. I am digging into Kotlin and love it! Have a few questions about the function composition.

I had to declare the functions before I could compose the functions (in your example they are below the composition.

And why did you use the Types A and B? You have a Double as input and a Double as output. Wouldn’t it be nicer to write it like that?

Sure you could reuse the compose function if the h-function for example would output a String. But I think in this example it’s misleading.

fun <A> compose(f: (A) -> A,
g: (A) -> A,
h: (A) -> A): (A) -> A = { x -> h(g(f(x))) }
taxed(value: Double): Double = value * 1.4
fun discounted(value: Double): Double = value * 0.9
fun rounded(value: Double): Double = Math.round(value).toDouble()

val prices = listOf(21.8, 232.5, 231.3)
val taxedDiscountedRounded = compose(::taxed, ::discounted, ::rounded)
Show your support

Clapping shows how much you appreciated Thomas Kay’s story.