raulraja
01/11/2018, 6:41 PMdata class CreditCard(val creditInfo: CreditCardInfo, val issuer: Person, val account: Account) {
companion object {
fun getPremium(totalCards: Int)(creditCard: CreditCard): Double = TODO() //note the two param groups
}
}
val creditCards: List<CreditCard> = getCreditCards()
// here the first param group is applied with _ resulting in a function that only needs the second param `creditcard`
val getPremiumWithTotal = CreditCard.getPremium(creditCards.length) _
val allPremiums = creditCards.map(getPremiumWithTotal).sum
Here we can decompose a bigger function CreditCard#getPremium
into smaller reusable functions getPremiumWithTotal
. As a bonus we are adapting the function to fit in List#map
as function reference getting simpler syntax.
I would also argue that with the incoming number of devs coming to Kotlin from other langs that are not Java and used to currying and partial application that's also frustrating to them.
Not having that frustration is as valid as arguing that Java folks are gonna be scared.
Currying and Partial Aplication are possible in Kotlin and cover many use cases but it's just boilerplate: https://github.com/arrow-kt/arrow/blob/master/arrow-syntax/src/main/kotlin/arrow/syntax/function/partials.kt
Something to consider about currying and Partial Application is that in Kotlin we have suspended functions in coroutines and I'm not positive how those would be impacted by such a feature