-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPartition.kt
29 lines (22 loc) · 853 Bytes
/
Partition.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.arindam.kotlin.x
/**
* Chop 🔪 chop 🔪 ... that's how we partition a list using a predicate 😉
*
* Created by Arindam Karmakar on 22/7/19.
*/
enum class Attendance { CONFIRMED, UNCONFIRMED }
data class Speaker(val name: String, val attendance: Attendance = Attendance.CONFIRMED)
fun main() {
val speakers = listOf(
Speaker("John"),
Speaker("Eric"),
Speaker("Santiago", Attendance.UNCONFIRMED)
)
val (confirmedSpeakers, unconfirmedSpeakers) = speakers.partition {
it.attendance == Attendance.CONFIRMED // (1/1) Partition 🔪 using a predicate!
}
// > [Speaker(name=John, attendance=CONFIRMED), Speaker(name=Eric, attendance=CONFIRMED)]
println(confirmedSpeakers)
// > [Speaker(name=Santiago, attendance=UNCONFIRMED)]
println(unconfirmedSpeakers)
}