Often, I have the following pattern:
a.x()
a.y()
a.z()
Kotlin offers a convenient alternative:
a.run { x(); y(); z() }
Sometimes I have a pattern like this:
a.x()
b.x()
c.x()
I want to write something like this:
applyTo(a, b, c) { it.x() }
So I might implement the following:
fun <P> applyTo(vararg ps: P, fx: (P) -> Unit) = ps.forEach { fx(it) }
Or alternately, something like this:
::x.eachOf(a, b, c)
So I could implement the function:
fun <P, R> ((P) -> R).eachOf(vararg p: P) = p.forEach { this(it) }
Is there a way to invoke a shared method on multiple receivers using the standard library, or a better way to shorten Pattern #2?