Hi, what is the “nicest” way to partially apply mo...
# arrow
d
Hi, what is the “nicest” way to partially apply more than a few parameters?
I know about two options:
Copy code
val renderSmsTemplate: RenderSmsTemplate = ::renderTemplate.curried()(
        generateCommandId
)(
        generateMessageId
)(
        getNow
)(
        TopicName(TEMPLATE_COMMANDS_TOPIC)
)(
        TemplateId(templateId)
)

val sendSms: SendSms = ::sendSms
        .partially1(generateCommandId)
        .partially1(generateMessageId)
        .partially1(getNow)
        .partially1(TopicName(SMS_COMMANDS_TOPIC))
Generally each function has the first parameters that are dependencies and the rest are actual parameters that are defined by some interface/type alias.
What I’m looking for is to apply a few leading parameters. Ideally:
Copy code
::sendSms.partially(generateCommandId, generateMessageId, getNow, TopicName(xx))
p
that’s the most interesting brackets i’ve seen in a long time 😅
😄 1
d
I have not found any other way to break the line 🙂
h
Hi, In the past I evaluated also the syntax
Copy code
(::sendSms)(generateCommandId, generateMessageId, getNow, TopicName(xx))
but maybe the most idiomatic way to do it in kotlin (one that every kotlin dev can understand immediately) is just
Copy code
{ sendSms(generateCommandId, generateMessageId, getNow, TopicName(xx)), it) }
or better
Copy code
{ templateId -> sendSms(generateCommandId, generateMessageId, getNow, TopicName(xx)), templateId) }
y
The issue is that you might have 5 other different parameters after that, or even worse you're gonna need to specify type parameters for them all.
I think just like how curried is defined we can also define a partial-application
invoke
that returns ethe appropriate function, but this'll potentially need a huge amount of definitions
p
If you already have the curried version of the function, you could write a generalized
uncurry4
function to partially apply the first 4 parameters in a single call.
Copy code
fun <A,B,C,D,R> ((A)->(B)->(C)->(D)->R).uncurry4(): (A,B,C,D)->R = 
{ a: A, b:B, c:C, d:D -> 
    this(a)(b)(c)(d) 
}
d
I was thinking about adding overloaded
partially
function that would apply
n
first arguments for all arities (1 - 22). But that means 253 * 2 functions (regular & suspend). I’m not sure if IDEA would be happy with that 🙂.
It’s sad that partial application is not supported by Kotlin.