Jakub
02/11/2020, 9:19 AMlist.add(item)
everywhere.
(I will use simple example)
I’ve tried to replace
(0..3).forEach {
list.add(it)
}
with
list.add(
(0..3).forEach {
it
}
)
However, it doesn’t compile. Is there a way to achieve it?Johannes Schamburger
02/11/2020, 9:21 AMlist.add
takes a single item. What you are looking for is probably `list.addAll`https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/add-all.htmlspand
02/11/2020, 9:21 AMlist.addAll(
(0..3).map {
it
}
)
Fleshgrinder
02/11/2020, 9:23 AMFleshgrinder
02/11/2020, 9:23 AMbuildList
(and friends) to Kotlin, should be available in the next patch release as experimental and non-experimental in the next minor release: https://github.com/JetBrains/kotlin/pull/2925Fleshgrinder
02/11/2020, 9:24 AMadd
in order to add something. Nothing smells here, this is simply how it works.Jakub
02/11/2020, 9:32 AMval items = mutableListOf<Item>()
days.groupBy { it.date.year }.forEach { (year, daysOfYear) ->
daysOfYear.groupBy { it.date.month }.forEach { (month, daysOfMonth) ->
items.add(
MonthItem("MonthItem$year$month", daysOfMonth.first().date, month.title(), month.background())
)
daysOfMonth.groupBy { it.date.weekNumber() }.forEach { (week, daysOfWeek) ->
items.add(
WeekItem("WeekItem$year$month$week", daysOfWeek.first().date, formatDateRange(daysOfWeek.first().date, daysOfWeek.last().date))
)
if (daysOfWeek.flatMap { it.events }.isEmpty()) {
items.add(
CreateEventItem("CreateEventItem${daysOfWeek}", daysOfWeek[0].date)
)
} else {
daysOfWeek.forEach { day ->
if (day.events.isEmpty()) {
items.add(
CreateEventItem("CreateEventItem${day}", day.date)
)
} else {
day.events.forEachIndexed { index, event ->
items.add(
EventItem("EventItem$day$event", day.date, event, index == 0)
)
}
}
}
}
}
}
}
Jakub
02/11/2020, 9:37 AMspand
02/11/2020, 9:42 AMJakub
02/11/2020, 9:57 AM