Function composition in standard library?


#1

In this GitHub gist https://gist.github.com/timyates/2300897 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?


#2

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: https://github.com/MarioAriasC/funKTionale


#3

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.


#4

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