is there a nice way to partially apply function in...
# functional
v
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
How’s this?
v
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
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
I see, thanks!
w
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