https://kotlinlang.org logo
#functional
Title
# functional
v

Vladyslav Sitalo

12/15/2020, 8:04 PM
is there a nice way to partially apply function in Kotlin to get another function. I’m looking for something like:
Copy code
fun manyParamFun(p1: Int, p2: String, p3: List<String>): String = ...
val newFun  = bind(::manyParamFun, p1=5) // to get (String, List<String>) -> String function
I know we can do things like
Copy code
fun newFun(p2: String, p3: List<String>): String = manyParamFun(5, p2, p3)
but I’m looking for a nicer way..
r

Rob

12/15/2020, 8:17 PM
How’s this?
v

Vladyslav Sitalo

12/16/2020, 4:09 AM
thanks for the suggestion! that can be an option, let me play with it. I’d like a version with arbitrary number of arguments though 🙂
r

Rob

12/16/2020, 4:10 PM
Unfortunately, there isn’t a way to make it work for any number of arguments without losing type information. Scala has the same limitations. I believe they generate their standard library code to handle up to an arbitrary number of parameters.
v

Vladyslav Sitalo

12/17/2020, 2:59 AM
I see, thanks!
w

wakingrufus

12/17/2020, 8:12 PM
Arrow Kt provides a curry extention function for Function2
Copy code
inline fun <A, B, Z> ((A, B) -> Z).curry(): (A) -> (B) -> Z = { p1: A -> { p2: B -> this(p1, p2) } }
👍 2
you could probably write your own based on that for higher arity
5 Views