https://kotlinlang.org logo
Title
m

miha-x64

04/24/2017, 10:49 AM
Any way to write this in funcitonal fashion?
controls.mapValues {
    val ans = EnumSet.noneOf(NotificationTarget::class.java)
    if (it.value.pushSwitch.isChecked) ans += NotificationTarget.PUSH
    if (it.value.emailSwitch.isChecked) ans += NotificationTarget.EMAIL
    ans
}
o

okkero

04/24/2017, 11:54 AM
controls.mapValues {
    EnumSet.copyOf(
            listOf(
                    it.value.pushSwitch to NotificationTarget.PUSH,
                    it.value.emailSwitch to NotificationTarget.EMAIL
            )
                    .filter { it.first.isChecked }
                    .map { it.second }
    )
}
how about that ^ ?
m

miha-x64

04/24/2017, 12:21 PM
Thank you, that’s an interesting approach, but too allocationful. There are many of switch pairs, and it’s not great to create extra map for each pair.
o

okkero

04/24/2017, 12:42 PM
If you're worried about the performance of the functional approach, then it might just be better to do it imperatively
1
Couldn't you just store the mappings in a top level immutable Map?
I guess you'd have to do an extra filter, then...hmm
m

miha-x64

04/24/2017, 12:44 PM
I thought about a global immutable map at the very first moment, but then I must tag actual switches somehow. Maybe I’d better stick with the original solution.
👍 1