[英]What parameters do I pass to 'filter' function in Scala
I am having trouble understanding this Scala code involving a lambda expression:我无法理解这个涉及 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))
}
It works, but the filter function takes an argument 's' that I didn't define.它有效,但过滤器 function 采用我没有定义的参数“s”。 What is 's'.什么是's'。 Also, if this were pandas, I think I would need to supply an axis=1 parameter.另外,如果这是 pandas,我想我需要提供一个 axis=1 参数。 Why don't I need it in Scala为什么我在 Scala 中不需要它
Perhaps it would help to compare Python implementation也许比较 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)]
with similar Scala implementation具有类似的 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))
where we see Scala lambda我们在哪里看到 Scala lambda
s => s.marks > 10
is similar to Python lambda类似于 Python lambda
lambda s: s.marks > 10
which should make it clear that declared lambda parameter s
becomes a local variable inside the body of the lambda.这应该清楚地表明,声明的 lambda 参数s
成为 lambda 主体内的局部变量。
Consider Scala API documentation for List#filter as well as Scala for Python Developers section of Scala 3 Book. Consider Scala API documentation for List#filter as well as Scala for Python Developers section of Scala 3 Book.
Note that the argument to the filter
method is not s
, but rather the entirety of s => s.marks > 10
: this is the syntax to define an anonymous function that takes some s
and returns the result of the comparison of its marks
field with the literal 10
.请注意, filter
方法的参数不是s
,而是整个s => s.marks > 10
:这是定义匿名function 的语法,它接受一些s
并返回其marks
字段的比较结果与文字10
。 In this context, s
is just the made up name of a parameter of the function, just like it would be if it was defined as a method as in the following example:在这种情况下, s
只是 function 的参数的组成名称,就像它被定义为以下示例中的方法一样:
def marksGreaterThan10(s: Student): Boolean = s.marks > 10
One thing that could make someone a bit confused about the syntax is that the compiler itself is able to infer the types based on your input, making it very compact and readable, but perhaps a bit opaque for newcomers.可能会让一些人对语法感到困惑的一件事是编译器本身能够根据您的输入推断类型,使其非常紧凑和可读,但对于新手来说可能有点不透明。 Note that without the appropriate context (ie a list of Student
s is being filtered, therefore the input type of the function must be a Student
), the compiler would complain about the missing parameter type
and you would need to provide one, as in the following example (where I assign the anonymous function to a variable, so that I can use it in some other part of the program):请注意,如果没有适当的上下文(即正在过滤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
. 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)
You can play around with the code above here on Scastie.您可以在 Scastie 上使用上面的代码。
If you are familiar with the Java lambda expression syntax, ->
is more or less equivalent to =>
and you would have written something like:如果您熟悉 Java lambda 表达式语法, ->
或多或少等同于=>
并且您会编写如下内容:
students.stream().filter(s -> s.marks > 10)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.