Scala 中的 apply 函数是什么? [英] What is the apply function in Scala?

查看:42
本文介绍了Scala 中的 apply 函数是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有从人为的解组和动词名词中理解它(AddTwo 类有一个 apply 添加两个!)示例.

I never understood it from the contrived unmarshalling and verbing nouns ( an AddTwo class has an apply that adds two!) examples.

我知道它是语法糖,所以(我从上下文推断)它一定是为了让一些代码更直观而设计的.

I understand that it's syntactic sugar, so (I deduced from context) it must have been designed to make some code more intuitive.

带有 apply 函数的类有什么意义?它的用途是什么,它使代码更好的用途是什么(解组、动词名词等)?

What meaning does a class with an apply function give? What is it used for, and what purposes does it make code better (unmarshalling, verbing nouns etc)?

在伴随对象中使用时它有什么帮助?

how does it help when used in a companion object?

推荐答案

数学家有他们自己的小有趣方法,所以与其说然后我们调用函数 f 传递它x 作为参数",正如我们程序员所说,他们谈论的是将函数 f 应用于其参数 x".

Mathematicians have their own little funny ways, so instead of saying "then we call function f passing it x as a parameter" as we programmers would say, they talk about "applying function f to its argument x".

在数学和计算机科学中,Apply 是一个应用函数函数到参数.
维基百科

In mathematics and computer science, Apply is a function that applies functions to arguments.
Wikipedia

apply 的目的是缩小 Scala 中面向对象范式和函数范式之间的差距.Scala 中的每个函数都可以表示为一个对象.每个函数也有一个 OO 类型:例如,一个接受 Int 参数并返回一个 Int 的函数将具有 Function1[Int,Int] 的 OO 类型.

apply serves the purpose of closing the gap between Object-Oriented and Functional paradigms in Scala. Every function in Scala can be represented as an object. Every function also has an OO type: for instance, a function that takes an Int parameter and returns an Int will have OO type of Function1[Int,Int].

 // define a function in scala
 (x:Int) => x + 1

 // assign an object representing the function to a variable
 val f = (x:Int) => x + 1

因为在 Scala 中一切都是对象 f 现在可以被视为对 Function1[Int,Int] 对象的引用.例如,我们可以调用继承自 AnytoString 方法,这对于纯函数来说是不可能的,因为函数没有方法:

Since everything is an object in Scala f can now be treated as a reference to Function1[Int,Int] object. For example, we can call toString method inherited from Any, that would have been impossible for a pure function, because functions don't have methods:

  f.toString

或者我们可以通过调用 f 上的 compose 方法并将两个不同的函数链接在一起来定义另一个 Function1[Int,Int] 对象:

Or we could define another Function1[Int,Int] object by calling compose method on f and chaining two different functions together:

 val f2 = f.compose((x:Int) => x - 1)

现在,如果我们想实际执行该函数,或者如数学家所说的将函数应用于其参数",我们将调用 Function1[Int,Int]<上的 apply 方法/code> 对象:

Now if we want to actually execute the function, or as mathematician say "apply a function to its arguments" we would call the apply method on the Function1[Int,Int] object:

 f2.apply(2)

每次要执行表示为对象的函数时都编写 f.apply(args) 是面向对象的方式,但会增加代码的混乱程度,而不会添加太多额外的内容如果能够使用更标准的符号,例如 f(args),那就太好了.这就是 Scala 编译器介入的地方,每当我们有对函数对象的引用 f 并编写 f (args) 以将参数应用于所表示的函数时,编译器会悄悄地扩展 f(args) 到对象方法调用 f.apply(args).

Writing f.apply(args) every time you want to execute a function represented as an object is the Object-Oriented way, but would add a lot of clutter to the code without adding much additional information and it would be nice to be able to use more standard notation, such as f(args). That's where Scala compiler steps in and whenever we have a reference f to a function object and write f (args) to apply arguments to the represented function the compiler silently expands f (args) to the object method call f.apply (args).

Scala 中的每个函数都可以被视为一个对象,它也可以以另一种方式工作 - 每个对象都可以被视为一个函数,只要它具有 apply 方法.这样的对象可以用在函数符号中:

Every function in Scala can be treated as an object and it works the other way too - every object can be treated as a function, provided it has the apply method. Such objects can be used in the function notation:

// we will be able to use this object as a function, as well as an object
object Foo {
  var y = 5
  def apply (x: Int) = x + y
}


Foo (1) // using Foo object in function notation 

当我们希望将对象视为函数时,有很多用例.最常见的场景是工厂模式.我们可以应用对象到一组参数来创建关联类的新实例,而不是使用工厂方法给代码添加混乱:

There are many usage cases when we would want to treat an object as a function. The most common scenario is a factory pattern. Instead of adding clutter to the code using a factory method we can apply object to a set of arguments to create a new instance of an associated class:

List(1,2,3) // same as List.apply(1,2,3) but less clutter, functional notation

// the way the factory method invocation would have looked
// in other languages with OO notation - needless clutter
List.instanceOf(1,2,3) 

所以 apply 方法只是缩小 Scala 中函数和对象之间差距的一种便捷方式.

So apply method is just a handy way of closing the gap between functions and objects in Scala.

这篇关于Scala 中的 apply 函数是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆