Skip to content

Instantly share code, notes, and snippets.

@himattm
Created August 5, 2021 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save himattm/0fa422f34ecd009c21bcdd9d58923751 to your computer and use it in GitHub Desktop.
Save himattm/0fa422f34ecd009c21bcdd9d58923751 to your computer and use it in GitHub Desktop.
The answer to the practice question asked in StoicallyTyped Issue 15 and answered in 16
data class Event(val title: String, val day: String)
fun main() {
// Create a list of our events
val events = listOf<Event>(
Event("New StoicallyTyped Newsletter", "Monday"),
Event("Workout", "Monday"),
Event("Conference", "Tuesday"),
Event("@AdamMc331 Streams", "Wednesday"),
Event("Car Wash", "Thursday"),
Event("Dinner Date", "Friday"),
Event("Appointment", "Monday"),
Event("Workout", "Wednesday"),
Event("Dentist", "Wednesday"),
Event("Workout", "Friday"),
Event("Free Coffee", "Tuesday"),
Event("Sleep In", "Saturday"),
Event("@codewiththeita Stream", "Sunday"),
)
// Use groupBy to group the events based on day
val groups = events.groupBy { event ->
event.day
}
// Print the events
println(groups)
}
main()
// Prints: (I formatted this to make the grouping easier to read rather than have all on one line.)
// {
// Monday=[
// Event(title=New StoicallyTyped Newsletter, day=Monday),
// Event(title=Workout, day=Monday),
// Event(title=Appointment, day=Monday)
// ],
// Tuesday=[
// Event(title=Conference, day=Tuesday),
// Event(title=Free Coffee, day=Tuesday)
// ],
// Wednesday=[
// Event(title=@AdamMc331 Streams, day=Wednesday),
// Event(title=Workout, day=Wednesday),
// Event(title=Dentist, day=Wednesday)
// ],
// Thursday=[
// Event(title=Car Wash, day=Thursday)
// ],
// Friday=[
// Event(title=Dinner Date, day=Friday),
// Event(title=Workout, day=Friday)
// ],
// Saturday=[
// Event(title=Sleep In, day=Saturday)
// ],
// Sunday=[
// Event(title=@codewiththeita Stream, day=Sunday)
// ]
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment