如果我有一个
case class User(var firstName: String, var lastName: String, var city: String)
和一份清单
val users = List(
new User("Peter", "Fox", "Berlin"),
new User("Otto", "Schmidt", "Berlin"),
new User("Carl", "Schmidt", "Berlin"),
new User("Hans", "Schmidt", "Berlin"),
new User("Hugo", "Schmidt", "Berlin"))
定义某事
val test1 = (user:User,key:String) => user.lastName.equals(key)
val test2 = (user:User,key:String) => user.firstName.startsWith(key)
和过滤
val test = users.filter(u => {
test1(u,"Schmidt") && test2(u,"H")
})
这工作正常。但是我怎样才能生成过滤 test1, test2 ... testn 的东西,动态地形成一个列表?我想预定义很多过滤条件并将它们组合成一个条件(如 test1(u,"Schmidt") && test2(u,"H"))来过滤我的列表并组合过滤顺序。