Scala-tuple和部分应用函数

Scala-tuple和部分应用函数,scala,Scala,一般来说,我对Scala和函数编程相当陌生,所以我在理解部分应用函数和函数转换的概念时遇到了一些困难。我很有可能会混淆一些术语,所以所有的更正都很感谢 注意:我使用的是Scala播放框架,但这更多的是Scala问题,而不是播放问题 给定这样的函数 def create(id: Long, userId: Long, label: String) scala> def f(a: Int, b: String) = 0 f: (a: Int, b: String)Int scala>

一般来说,我对Scala和函数编程相当陌生,所以我在理解部分应用函数和函数转换的概念时遇到了一些困难。我很有可能会混淆一些术语,所以所有的更正都很感谢

注意:我使用的是Scala播放框架,但这更多的是Scala问题,而不是播放问题

给定这样的函数

def create(id: Long, userId: Long, label: String)
scala> def f(a: Int, b: String) = 0
f: (a: Int, b: String)Int

scala> (f _).tupled((2, "Hello"))
res0: Int = 0
scala> def f(a: Int, b: String) = 0
f: (a: Int, b: String)Int

scala> val ff = f(_: Int, "Hello")
ff: Int => Int = <function1>

scala> ff(2)
res1: Int = 0
scala> def g(a: Long, b: Int, c: String) = 0
g: (a: Long, b: Int, c: String)Int

scala> val h = g(1, _: Int, _: String)
h: (Int, String) => Int = <function2>

scala> (h _).tupled((2, "Hello"))
我将
userId
label
作为
(Int,String)
元组,将
id
作为
Long
。基本上,我要做的是同时将id和元组传递给函数

现在,我已经读到了,将元组传递给函数可以这样做

def create(id: Long, userId: Long, label: String)
scala> def f(a: Int, b: String) = 0
f: (a: Int, b: String)Int

scala> (f _).tupled((2, "Hello"))
res0: Int = 0
scala> def f(a: Int, b: String) = 0
f: (a: Int, b: String)Int

scala> val ff = f(_: Int, "Hello")
ff: Int => Int = <function1>

scala> ff(2)
res1: Int = 0
scala> def g(a: Long, b: Int, c: String) = 0
g: (a: Long, b: Int, c: String)Int

scala> val h = g(1, _: Int, _: String)
h: (Int, String) => Int = <function2>

scala> (h _).tupled((2, "Hello"))
也可以部分应用这样的函数

def create(id: Long, userId: Long, label: String)
scala> def f(a: Int, b: String) = 0
f: (a: Int, b: String)Int

scala> (f _).tupled((2, "Hello"))
res0: Int = 0
scala> def f(a: Int, b: String) = 0
f: (a: Int, b: String)Int

scala> val ff = f(_: Int, "Hello")
ff: Int => Int = <function1>

scala> ff(2)
res1: Int = 0
scala> def g(a: Long, b: Int, c: String) = 0
g: (a: Long, b: Int, c: String)Int

scala> val h = g(1, _: Int, _: String)
h: (Int, String) => Int = <function2>

scala> (h _).tupled((2, "Hello"))
scala>def(a:Int,b:String)=0
f:(a:Int,b:String)Int
scala>val ff=f(u:Int,“Hello”)
ff:Int=>Int=
scala>ff(2)
res1:Int=0
我最初的想法是把这两个概念结合起来

def create(id: Long, userId: Long, label: String)
scala> def f(a: Int, b: String) = 0
f: (a: Int, b: String)Int

scala> (f _).tupled((2, "Hello"))
res0: Int = 0
scala> def f(a: Int, b: String) = 0
f: (a: Int, b: String)Int

scala> val ff = f(_: Int, "Hello")
ff: Int => Int = <function1>

scala> ff(2)
res1: Int = 0
scala> def g(a: Long, b: Int, c: String) = 0
g: (a: Long, b: Int, c: String)Int

scala> val h = g(1, _: Int, _: String)
h: (Int, String) => Int = <function2>

scala> (h _).tupled((2, "Hello"))
scala>def g(a:Long,b:Int,c:String)=0
g:(a:Long,b:Int,c:String)Int
scala>valh=g(1,u:Int,u:String)
h:(Int,String)=>Int=
scala>(h_u2;).tuple((2,“Hello”))
但是,这会导致一个错误

<console>:10: error: _ must follow method; cannot follow h.type
              (h _).tupled((1, "Hello"))
               ^
:10:错误:\必须遵循方法;不能跟在h.type后面
(h_u).tuple((1,“Hello”))
^
所以我的问题是,首先为什么这不起作用,因为对我来说这是有意义的。第二,我将如何实现这一效果


谢谢你的帮助

不要将其抽象两次:不要对
h
进行冗余和下划线,因为它在部分应用后已经是一个函数了:

scala> def create(a: Long, b: Int, c: String) = 0
create: (a: Long, b: Int, c: String)Int

scala> val h = create(1, _: Int, _: String)
h: (Int, String) => Int = <function2>

scala> h.tupled((1, "Hello"))
res0: Int = 0
scala>def create(a:Long,b:Int,c:String)=0
创建:(a:Long,b:Int,c:String)Int
scala>valh=create(1,u0:Int,0:String)
h:(Int,String)=>Int=
scala>h.tuple((1,“Hello”))
res0:Int=0
更深入地说,
tuple
是在函数上定义的(比如
Int=>String
),而不是在方法上定义的(比如
def(i:Int):String
)-所以有时候你需要将一个方法转换成函数-它被称为eta扩展(或者更一般地说是eta抽象)。当您执行部分应用时-eta扩展将自动完成(您已经有一个
(Int,String)=>Int
)-因此您不必执行两次


有关更多信息,请参阅。

谢谢,它很有效!看来关于Scala我还有很多东西要学。不客气!关于Scala,总有很多东西需要学习:)