Function composition in standard library?

In this GitHub gist Composing functions in Kotlin · GitHub it is stated that in order to have function composition, one has to include the code:

infix public fun<V, T, R> Function1<T, R>.compose(before: (V) -> T): (V) -> R {
    return { v: V -> this(before(v)) }

This works, but I would have thought function composition was so important it would have been in the standard library. Is it and I have just missed it?


No, it’s not in the standard library. Function composition is very important in certain programming styles, and almost never occurs in others.

You can find many functional programming primitives for Kotlin in the Funktionale library: GitHub - MarioAriasC/funKTionale: Functional constructs for Kotlin

1 Like

I appreciate some people never use function composition, I just think it ought to be in the standard library. It isn’t but no worries I have the function needed. I was aware of funKTionale, I just didn’t want the extra dependency just for the one function. Question answered; thanks for doing so promptly, very much appreciated.

1 Like

funKTionale author here. I’m planning to release a modular version.

A GOTY edition with everything and several jars for basic stuff if you don’t need all

1 Like