Nick
05/29/2022, 7:20 PMstartDate
and an endDate
. I’d like to determine which events overlap so I can place events in a different column. Is there a way to apply some sort of groupBy
function to accomplish a way for me to group them?ephemient
05/29/2022, 7:30 PMNick
05/29/2022, 7:31 PMephemient
05/29/2022, 7:31 PMasad.awadia
05/30/2022, 3:16 PMNick
05/30/2022, 3:16 PMandries.fc
05/31/2022, 9:34 AMNick
05/31/2022, 2:29 PMdata class Event(val id: Int, val startDate: LocalDate, val endDate: LocalDate, val name: String)
How would I use your function?andries.fc
05/31/2022, 2:30 PMNick
06/01/2022, 6:57 AMandries.fc
06/01/2022, 6:58 AMEvent.overlap()
function before just using it. did not write any test cases.ephemient
06/01/2022, 7:08 AMfun Event.overlap(other: Event): Boolean = startDate <= other.endDate && endDate >= other.startDate
b. there's no reason to use a fold,
val overlappingEvents = buildList {
for (event in events) // ...
}
c. the approach is just wrong,
val events = listOf(
Event(1, LocalDate("2022-04-20"), LocalDate("2022-04-23"), "event-1"),
Event(2, LocalDate("2022-04-20"), LocalDate("2022-04-21"), "event-2"),
Event(3, LocalDate("2022-04-22"), LocalDate("2022-04-23"), "event-3"),
).sortedBy(Event::startDate)
returns two disjoint groups instead of one