Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 什么';这是PartialFunction和Function1之间的关系_Scala - Fatal编程技术网

Scala 什么';这是PartialFunction和Function1之间的关系

Scala 什么';这是PartialFunction和Function1之间的关系,scala,Scala,我正在学习Scala,从Scala文档中可以找到PartialFunction和Function1的定义,如下所示: trait PartialFunction[-A, +B] extends (A) ⇒ B trait Function1[-T1, +R] extends AnyRef scala> val process = (f: Function1[String, Int]) => f("1024") process: (String => Int) => In

我正在学习Scala,从Scala文档中可以找到PartialFunction和Function1的定义,如下所示:

trait PartialFunction[-A, +B] extends (A) ⇒ B
trait Function1[-T1, +R] extends AnyRef
scala> val process = (f: Function1[String, Int]) => f("1024")
process: (String => Int) => Int = <function1>

scala> val pattern = "([0-9]+)".r
pattern: scala.util.matching.Regex = ([0-9]+)

scala> val str2int: PartialFunction[String, Int] = {
     | case pattern(num) => num.toInt
     | }
str2int: PartialFunction[String,Int] = <function1>

scala> accept(str2int)
res67: Int = 1024
问题1)我的第一个问题是:(A)=>B的类型是什么

我知道,我们可以用提升法把部分函数变成正规函数

但问题2)对等函数和函数1之间的关系是什么

如果某个函数参数的类型为Function1,我们可以向其传递一个匹配的PartitionFunction,如下所示:

trait PartialFunction[-A, +B] extends (A) ⇒ B
trait Function1[-T1, +R] extends AnyRef
scala> val process = (f: Function1[String, Int]) => f("1024")
process: (String => Int) => Int = <function1>

scala> val pattern = "([0-9]+)".r
pattern: scala.util.matching.Regex = ([0-9]+)

scala> val str2int: PartialFunction[String, Int] = {
     | case pattern(num) => num.toInt
     | }
str2int: PartialFunction[String,Int] = <function1>

scala> accept(str2int)
res67: Int = 1024
scala>val进程=(f:Function1[String,Int])=>f(“1024”)
进程:(字符串=>Int)=>Int=
scala>val pattern=“([0-9]+)”。r
模式:scala.util.matching.Regex=([0-9]+)
scala>val str2int:PartialFunction[String,Int]={
|案例模式(num)=>num.toInt
| }
str2int:PartialFunction[String,Int]=
scala>accept(str2int)
res67:Int=1024

谢谢

A⇒ B
函数1[A,B]
的语法糖。类似地,
(A1、A2)⇒ R
实际上是
函数2[A1、A2、R]
等,一直到22(完全任意限制)。因此,
PartialFunction
的定义是

trait PartialFunction[-A, +B] extends Function1[A, B]
由于
部分函数[a,B]
也是
函数1[a,B]
,因此可以将其传递到需要
a的对象中⇒ B
。我们使用
覆盖
功能n
美观:它看起来更漂亮。事实上,因为
不是一个真正的类型名称,我们不能这样说:

type ApIntInt[T[_, _]] = T[Int, Int]
// ApIntInt[⇒] // Error: ⇒ is not a type and was not expected here
ApIntInt[Function1] // Fine: Function1 is a type, it has the right kind, so it works.
// ApIntInt[Function1] = Function1[Int, Int] = Int ⇒ Int
因为你是一个初学者,在很长一段时间内你不会看到这种东西(更高级的种类),但它在那里,你很可能有一天会找到它

当您将
部分函数
用作
函数1
时,如果您传递了一个未定义的值,它(可能)会抛出一个异常,这通常是
匹配错误
(但不必如此)。相反,如果调用
pf.lift
,则会创建一个
函数[In,Option[Out]]
,如果
部分函数
在某个点定义,则返回
Some(result)
,如果不是,则根据Scaladoc返回
None

例: