https://kotlinlang.org logo
Title
i

ivano

11/05/2021, 1:24 PM
Hi this is horrible do you know a more elegant way to refactor that ? ( sorry cor the format i am on mobile the three format ``` are not picked up) if (count() > 5) take(5).forEach { firstFiveSubsectionItems -> buildAlwaysVisibleLogic(alwaysVisibleView, firstFiveSubsectionItems) } else { forEach { itemsSubsection -> buildAlwaysVisibleLogic(alwaysVisibleView, itemsSubsection) } }
s

sbyrne

11/05/2021, 1:32 PM
You can do
take(5)
even if there are fewer than 5, and you'll just get all of them.
i

ivano

11/05/2021, 1:39 PM
Doh thanks!
a

Astronaut4449

11/07/2021, 5:55 AM
In case
take
or any other function did not work for both cases you should write it like this:
val items = if (condition) a() else b()
items.forEach { commonAction(it) }
1
i

ivano

11/07/2021, 11:33 AM
Thanks