简体   繁体   English

我将哪些参数传递给 Scala 中的“过滤器”function

[英]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.

相关问题 我可以在tkraise()函数中传递哪些参数? - What parameters can I pass in the tkraise() function? 如何将参数从guizero对象传递给函数? - How do I pass parameters to a function from a guizero object? 如何将可变长度参数传递给另一个函数的参数? - How do I pass variable length arguments to parameters of another function? 我需要什么样的参数才能发送此功能才能正常工作 - what kind of parameters do i need to send this function for it to work 如何从 sqlalchemy 过滤器语句中的列表中传递多个参数 - How do i pass multiple parameters from list in sqlalchemy filter statement 我在tf.contrib.learn.LinearClassifier.fit中作为x和y参数传递什么 - What do I pass as the x and y parameters in tf.contrib.learn.LinearClassifier.fit 将输入参数传递给Theano函数的正确方法是什么? - What is the right way to pass inputs parameters to a Theano function? 我应该怎么做才能使用字典映射 filter() function 结果? - What should I do so that I can use a dictionary mapping the filter() function result? 我怎么知道wingware IDE使用python函数需要什么参数 - how do i know what parameters a python function takes with wingware IDE 如何将参数传递给作为关键字参数传递的函数? - How do you pass parameters to a function passed as a keyword argument?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM