了解什么是功能

时间:2019-11-27 06:56:36

标签: scala function

背景

我试图回答一个问题“什么是功能?”想知道我是否真的知道它是什么。请帮助了解Scala中的“功能”是什么。听起来像是胡说八道,但请耐心等待。

问题

1。什么是功能

“功能”是要“应用于”单个“参数”以生成“值”的“计算/运算”。如果有多个参数,则可以将其转换为()()()...,称为currying。

  • w是一个名字
  • b是功能对象与名称的绑定
  • c是计算
  • 一个是应用程序
  • g是对其进行计算的参数
val w   =  ( )  => { }
    ^   ^   ^   ^   ^
    |   |   |   |   |
   (n) (b) (g) (a) (c)

可以说这些吗?

如果这是将计算应用于参数:

() => { }

那它实际上应该朝相反的方向吗?

() <= { }
or 
{ } => ()

2。分解定义

对“ def f(x:Unit):Unit = {}”的理解正确吗?

//--------------------------------------------------------------------------------
// The function literal says:
// 1. Define a "function" (ignore "method" here).
// 2. Bind the function to a name f. 
// 3. It is to be applied to an "argument" of type Unit.
// 4. Bind the argument to a name x.
// 5. E-valuation, or appliation of the function to an argument generates an "value" of type Unit.
// 6. After evaluation, substitute it with the "value".
//--------------------------------------------------------------------------------
def f (x:Unit):Unit = {}

3。评估/应用

“求值”与“将函数应用于参数并产生值”一样吗?当我阅读lambda calculas时,使用了“应用程序”一词,但我认为也使用了“评估”。

单位

//--------------------------------------------------------------------------------
// The literal says:
// 1. Apply the function f
// 2. on the "argument" enclosed between '(' and ')', which is Unit.
// 3. and yield Unit as the "value" evaluated.
//--------------------------------------------------------------------------------
def f (x:Unit):Unit = {} 
f()

与此相同吗?如果是,“单元”是否是对象?

f(Unit)  // No error in Scala worksheet

是什么原因导致作为以下参数的单元错误“参数太多”?

// Define a function that applies on an argument x of type Unit to generate Unit
() => {}     // res0: () => Unit = <function0>
(() => {})   // res1: () => Unit = <function0>

// Application
(() => {})()

/* Error: Too many arguments */
(() => {})(Unit)

4。参照透明度

请告知这是否正确。

以“ def g(x:String):Unit = println(x)”为例,“参照透明性”意味着g(x)始终可以用其结果替换,并且不会破坏任何结果。

如果

g("foo")

可以始终替换为

Unit

然后它是参照透明的。但是,这里不是g。因此g不是参照透明函数,因此不是“纯”函数。随机也不是纯粹的。

{ scala.util.Random.nextInt } // res0: Int = -487407277

在Scala中,功能可以是纯功能或副作用功能。仅看一个函数就无法分辨。还是有办法这样标记或验证其是否纯净?

5。方法不是函数

方法不能是要传递的第一类对象,但可以通过将其转换为函数来实现。

def g (x:String): Unit = println(x)
g("foo")

val _g = g _
_g("foo")

为什么方法不能是一流的对象?如果一个方法是一个对象,将会发生什么或会破坏什么?

Scala编译器会进行聪明的推断或补充,那么如果可以使用 _ 将其转换为对象,为什么Scala不会将其设为firt类对象?

6。什么是{...}?

更新

“ => T”是通过名称传递的表达式调用的,以在函数内部求值,因此与“ {...}”无关。 {...}是一个块表达式。因此,以下所有内容均无效。

看起来“ {...}”与“ => T”相同。

  

def fill [T](n:Int)(elem:=> T)

Array.fill[Int](3)({ scala.util.Random.nextInt })

{...}本身会产生一个值,而无需接受任何参数。

{ scala.util.Random.nextInt }    // res0: Int = 951666328
{ 1 }                            // res1: Int = 1

这是否意味着“应用程序”是一个独立的一流对象,或者Scala编译器足够聪明以至于无法理解它是

的缩写。
() => { scala.util.Random.nextInt }
or 
val next = (x:Int) => { scala.util.Random.nextInt(x) }

如果是这样,“ => T”实际上是“()=> T”吗?

1 个答案:

答案 0 :(得分:0)

在Scala中,函数是根据输入参数量从Function1Function22的特征之一的实现。对于您的特定示例,wanonfunW的简写:

val w = () => {}
val anonfunW = new Function1[Unit, Unit] {
    def apply(x: Unit): Unit = ()
  }