Any way to write this in funcitonal fashion? ``` c...
# getting-started
m
Any way to write this in funcitonal fashion?
Copy code
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
Copy code
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
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
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
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