[英]What parameters do I pass to 'filter' function in Scala
我无法理解这个涉及 lambda 表达式的 Scala 代码:
object ScalaList {
case class Student(rollNo : Int, name : String, marks : Int)
val students= List(Student(1,"A",10),Student(2,"B",14))
var toppers = students.filter(s=>s.marks>10) //> toppers : List[ScalaList.Student] = List(Student(2,B,14))
}
它有效,但过滤器 function 采用我没有定义的参数“s”。 什么是's'。 另外,如果这是 pandas,我想我需要提供一个 axis=1 参数。 为什么我在 Scala 中不需要它
也许比较 Python 实现会有所帮助
>>> from pydantic import BaseModel
>>>
>>> class Student(BaseModel):
... rollNo: int
... name: str
... marks: int
...
>>> students = [
... Student(rollNo=1, name="A", marks=10),
... Student(rollNo=2, name="B", marks=14)
... ]
>>>
>>> list(filter(lambda s: s.marks > 10, students))
[Student(rollNo=2, name='B', marks=14)]
具有类似的 Scala 实现
scala> case class Student(
| rollNo: Int,
| name: String,
| marks: Int
| )
| val students = List(
| Student(1, "A", 10),
| Student(2, "B", 14)
| )
| students.filter(s => s.marks > 10)
class Student
val students: List[Student] = List(Student(1,A,10), Student(2,B,14))
val res0: List[Student] = List(Student(2,B,14))
我们在哪里看到 Scala lambda
s => s.marks > 10
类似于 Python lambda
lambda s: s.marks > 10
这应该清楚地表明,声明的 lambda 参数s
成为 lambda 主体内的局部变量。
Consider Scala API documentation for List#filter as well as Scala for Python Developers section of Scala 3 Book.
请注意, filter
方法的参数不是s
,而是整个s => s.marks > 10
:这是定义匿名function 的语法,它接受一些s
并返回其marks
字段的比较结果与文字10
。 在这种情况下, s
只是 function 的参数的组成名称,就像它被定义为以下示例中的方法一样:
def marksGreaterThan10(s: Student): Boolean = s.marks > 10
可能会让一些人对语法感到困惑的一件事是编译器本身能够根据您的输入推断类型,使其非常紧凑和可读,但对于新手来说可能有点不透明。 请注意,如果没有适当的上下文(即正在过滤Student
的列表,因此 function 的输入类型必须是Student
),编译器会抱怨missing parameter type
,您需要提供一个,如以下示例(我将匿名 function分配给变量,以便我可以在程序的其他部分使用它):
val marksGreaterThan10 = (s: Student) => s.marks > 10
Note that both a method and a function can be passed to a higher-order function (a function that can take a function as a parameter or return one as an output) like filter
.
final case class Student(marks: Int)
val marksGreaterThan10Function = (s: Student) => s.marks > 10
def marksGreaterThan10Method(s: Student): Boolean = s.marks > 10
val students = Seq(Student(1), Student(11))
// Both of these returns `Seq(Student(11))`
students.filter(marksGreaterThan10Function)
students.filter(marksGreaterThan10Method)
您可以在 Scastie 上使用上面的代码。
如果您熟悉 Java lambda 表达式语法, ->
或多或少等同于=>
并且您会编写如下内容:
students.stream().filter(s -> s.marks > 10)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.