Kotlin Function Application

I often write some code like this:

val result = someData.split(...)
        .map { ... }
        .filter { ... }
        .reduce { ... }
        ....

someFunction(result)

As you can see last line of the code is breaking the beautiful flow of chained functions. One can rewrite this as:

someFunction(someData.split(...)
        .map { ... }
        .filter { ... }
        .reduce { ... }
        ....)

Which seems better to me but not as good as this:

someData.split(...)
        .map { ... }
        .filter { ... }
        .reduce { ... }
        ....
        .apply(::someFunction)

I don’t know if there is a standard way of doing this but here is my solution:

infix fun <T, R> T.apply(func: (T) -> R): R = func(this)

So this extension function applies its object to the function that it took as an argument and returns the result of application. You can use it as an infix operator, if you want to:

someData.split(...)
        .map { ... }
        .filter { ... }
        .reduce { ... }
        .... apply ::someFunction

You can even chain function applications:

someData.split(...)
        .map { ... }
        .filter { ... }
        .reduce { ... }
        ....
        .apply(::fun1)
        .apply(::fun2)
        .apply(::fun3)
        .apply { fun4(it) }

Which is same as:

someData.split(...)
        .map { ... }
        .filter { ... }
        .reduce { ... }
        .... apply ::fun1 apply ::fun2 apply ::fun3 apply { fun4(it) }

This code is equivalent of this one:

val result = someData.split(...)
        .map { ... }
        .filter { ... }
        .reduce { ... }
        ....

fun4(fun3(fun2(fun1(result))))

Leave a Reply