Not exactly what OP wants, but it seems like the issue here is comparing between two objects of type Student
rather than chaining predicates.
Not sure what the use case is but here's a more object-oriented solution, where we park the predicates under Student::isSimilarToJohn
(because I'm assuming this John Smith is pretty special):
data class Student(
val firstName: String?,
val lastName: String?,
val age: Int?,
val homeAddress: String?,
val cellPhone: String?,
) {
fun isSimilarToJohn(): Boolean {
return firstName == "John" &&
lastName == "Smith" &&
age == 20 &&
homeAddress == "45 Boot Terrace" &&
cellPhone.orEmpty().startsWith("123456")
}
}
Example:
val students = listOf(
Student("John", "Smith", 20, "45 Boot Terrace", "1234568"),
Student("John", "Smith", 20, "45 Boot Terrace", "1234567"),
Student("Mary", "Smith", 20, "45 Boot Terrace", "1234567"),
Student("John", "Doe", 20, "45 Boot Terrace", "1234567"),
)
students.map { it.isSimilarToJohn() }
// [true, true, false, false]