Is there any way to combine two functions to make ...
# announcements
r
Is there any way to combine two functions to make a third function such as this?
Copy code
val function1: (Int, String, Byte) -> String = { i: Int, s: String, byte: Byte -> "example" }

val function2: (String) -> Int = { -1 }

//to get something like 
val function3: (Int, String, Byte) -> Int = function1.then(function2)
obviously the “then” function is fictional otherwise i wouldn’t ask. I’d love to be able to do this without actually having to know function1's arguments explicitly… just that it returns a type that matches the arguments of function2
r
infix fun <A, B, C, D, E>((A, B, C) -> D).compose(after: (D) -> E) = after(this())
? purely hand-written, no guarantee compileable.
and i think arrow may have the hole composition of these signatures, for different functions. https://arrow-kt.io
r
yeah I figured it’d be something arrow would want. I’m trying to stay multiplatform compatible and last i checked arrow isn’t there yet 🤔 Good lead, thanks
r
Copy code
infix fun <A, B, C, D, E>((A, B, C) -> D).compose(after: (D) -> E) = { a: A, b: B, c: C -> after(this(a,b,c)) }
this is compiling and working
r
so I’ll have to have versions of that function for every number of parameters eh
the secret sauce I want is to be able to convert a data class constructor function to a function that returns something else, and capitalize on the autogenerated data class stuff
r
wtb multiplatform arrow! 🙂