Scala 中的函数文字是什么?

发布于 2024-10-20 17:25:50 字数 33 浏览 1 评论 0原文

Scala 中的函数文字是什么以及何时应该使用它们?

What is a function literal in Scala and when should I use them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅浅淡淡 2024-10-27 17:25:50

函数文字是定义函数的替代语法。当您想要将函数作为参数传递给方法(尤其是折叠或过滤操作等高阶函数)但又不想定义单独的函数时,它非常有用。函数文字是匿名的——默认情况下它们没有名称,但您可以通过将它们绑定到变量来给它们命名。函数字面量的定义如下:

(a:Int, b:Int) => a + b

您可以将它们绑定到变量:

val add = (a:Int, b:Int) => a + b
add(1, 2) // Result is 3

就像我之前所说,函数字面量对于将参数传递给高阶函数非常有用。它们对于定义嵌套在其他函数中的单行函数或辅助函数也很有用。

A Tour of Scala 为函数文字提供了很好的参考(他们称之为匿名函数) 。

A function literal is an alternate syntax for defining a function. It's useful for when you want to pass a function as an argument to a method (especially a higher-order one like a fold or a filter operation) but you don't want to define a separate function. Function literals are anonymous -- they don't have a name by default, but you can give them a name by binding them to a variable. A function literal is defined like so:

(a:Int, b:Int) => a + b

You can bind them to variables:

val add = (a:Int, b:Int) => a + b
add(1, 2) // Result is 3

Like I said before, function literals are useful for passing as arguments to higher-order functions. They're also useful for defining one-liners or helper functions nested within other functions.

A Tour of Scala gives a pretty good reference for function literals (they call them anonymous functions).

野侃 2024-10-27 17:25:50

将函数文字与 Scala 中其他类型的文字进行比较可能很有用。 文字是表示某些类型的值的符号糖。语言认为特别重要。 Scala 有 整数文字、字符文字、字符串文字等。Scala 将函数视为可在源代码中通过函数文字表示的第一类值。这些函数值属于特殊的 函数类型。例如,

  • 5 是表示 Int 类型中的值的整数文字
  • 'a' 是表示 Char 中的值的字符文字 类型
  • (x: Int) => x + 2 是一个函数文字,表示 Int =>; 中的值Int 函数类型

文字通常用作匿名值,即不首先将它们绑定到命名变量。这有助于使程序更加简洁,并且适用于文字不可重用的情况。例如:

List(1,2,3).filter((x: Int) => x > 2)

对比

val one: Int = 1
val two: Int = 2
val three: Int = 3
val greaterThan2: Int => Boolean = (x: Int) => x > two
List(one,two,three).filter(greaterThan2)

It might be useful to compare function literals to other kinds of literals in Scala. Literals are notational sugar for representing values of some types the language considers particularly important. Scala has integer literals, character literals, string literals, etc. Scala treats functions as first class values representable in source code by function literals. These function values inhabit a special function type. For example,

  • 5 is an integer literal representing a value in Int type
  • 'a' is a character literal representing a value in Char type
  • (x: Int) => x + 2 is a function literal representing a value in Int => Int function type

Literals are often used as anonymous values, that is, without bounding them to a named variable first. This helps make the program more concise and is appropriate when the literal is not meant to be reusable. For example:

List(1,2,3).filter((x: Int) => x > 2)

vs.

val one: Int = 1
val two: Int = 2
val three: Int = 3
val greaterThan2: Int => Boolean = (x: Int) => x > two
List(one,two,three).filter(greaterThan2)
2024-10-27 17:25:50

Scala 编程,第三版

8.3 一流函数

函数文字被编译成一个类,在运行时实例化时它是一个函数值。[2]因此,函数字面量和值之间的区别在于函数字面量存在于源代码中,而函数值在运行时作为对象存在。这种区别很像类(源代码)和对象(运行时)之间的区别。

Programming in Scala, Third Edition

8.3 FIRST-CLASS FUNCTIONS

A function literal is compiled into a class that when instantiated at runtime is a function value.[2] Thus the distinction between function literals and values is that function literals exist in the source code, whereas function values exist as objects at runtime. The distinction is much like that between classes (source code) and objects (runtime).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文