https://kotlinlang.org logo
Title
d

david-wg2

04/04/2019, 2:53 PM
items.onEach
?
l

Luke

04/04/2019, 2:54 PM
onEach
still works with
it
d

david-wg2

04/04/2019, 2:55 PM
i guess this depends on what you have in your
apply
d

Dias

04/04/2019, 3:10 PM
do you want to treat each element as
this
?
l

Luke

04/04/2019, 3:14 PM
Yes, I want to apply something to every element of my collection. So if I could write
items.applyOnEach { this.attribute = "something" }
, that would be great
The method might no exist. I did not find anything worthwhile by googling
d

Dias

04/04/2019, 3:38 PM
probably doesn't, just create your own extension
fun <T> Collection<T>.applyOnEach(block: T.() -> Unit) {
    this.forEach {
        it.apply(block)
    }
}
a

Alowaniak

04/04/2019, 3:55 PM
Or maybe make extension function on
it
to do the applying and pass that to the forEach
l

Luke

04/04/2019, 4:10 PM
Yes absolutely, this is easily implementable. I’m just asking if it already exist. I don’t want to add noise in my code
z

zhuinden

04/04/2019, 4:39 PM
I mean, you could do
inline fun <T> Collection<T>.forEachApply(block: T.() -> Unit
like said above, and it would work great
n

nkiesel

04/05/2019, 6:47 AM
items.map { it.attribute = "something" }