我如何得到 (a, b) => c 来自 a => b =>斯卡拉中的c?

发布于 2024-09-13 16:09:12 字数 379 浏览 9 评论 0原文

如果我有:

val f : A => B => C

这是以下简写:

val f : Function1[A, Function1[B, C]]

如何获得带有签名的函数g:(

val g : (A, B) => C = error("todo")

即)

val g : Function2[A, B, C] //or possibly
val g : Function1[(A, B), C]

f表示?

If I have:

val f : A => B => C

This is shorthand for:

val f : Function1[A, Function1[B, C]]

How do I get a function g with the signature:

val g : (A, B) => C = error("todo")

(i.e.)

val g : Function2[A, B, C] //or possibly
val g : Function1[(A, B), C]

in terms of f?

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

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

发布评论

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

评论(4

ペ泪落弦音 2024-09-20 16:09:12
scala> val f : Int => Int => Int = a => b => a + b
f: (Int) => (Int) => Int = <function1>

scala> Function.uncurried(f)
res0: (Int, Int) => Int = <function2>
scala> val f : Int => Int => Int = a => b => a + b
f: (Int) => (Int) => Int = <function1>

scala> Function.uncurried(f)
res0: (Int, Int) => Int = <function2>
不念旧人 2024-09-20 16:09:12

为了完整性,扩展 retonym 的答案

val f : Int => Int => Int = a => b => a + b
val g: (Int, Int) => Int = Function.uncurried(f)
val h: ((Int, Int)) => Int = Function.tupled(g)

这两个操作的逆函数也在 Function 对象上提供,因此如果您愿意,您可以向后编写上面的内容

val h: ((Int, Int)) => Int =  x =>(x._1 + x._2)
val g: (Int, Int) => Int = Function.untupled(h)
val f : Int => Int => Int = g.curried  //Function.curried(g) would also work, but is deprecated. Wierd

Extending retonym's answer, for completeness

val f : Int => Int => Int = a => b => a + b
val g: (Int, Int) => Int = Function.uncurried(f)
val h: ((Int, Int)) => Int = Function.tupled(g)

The converse functions for both of these operations are also provided on the Function object, so you could write the above backwards, if you wished

val h: ((Int, Int)) => Int =  x =>(x._1 + x._2)
val g: (Int, Int) => Int = Function.untupled(h)
val f : Int => Int => Int = g.curried  //Function.curried(g) would also work, but is deprecated. Wierd
多情癖 2024-09-20 16:09:12

只是为了完善答案,尽管有一个库方法可以执行此操作,但手动执行此操作也可能具有指导意义:

scala> val f = (i: Int) => ((s: String) => i*s.length)
f: (Int) => (String) => Int = <function1>

scala> val g = (i: Int, s: String) => f(i)(s)
g: (Int, String) => Int = <function2>

或者一般来说,

def uncurry[A,B,C](f: A=>B=>C): (A,B)=>C = {
  (a: A, b: B) => f(a)(b)
}

Just to round out the answer, although there is a library method to do this, it may also be instructive to do it by hand:

scala> val f = (i: Int) => ((s: String) => i*s.length)
f: (Int) => (String) => Int = <function1>

scala> val g = (i: Int, s: String) => f(i)(s)
g: (Int, String) => Int = <function2>

Or in general,

def uncurry[A,B,C](f: A=>B=>C): (A,B)=>C = {
  (a: A, b: B) => f(a)(b)
}
酒中人 2024-09-20 16:09:12

与 Rex Kerr 的答案类似,但更容易阅读。

type A = String
type B = Int
type C = Boolean

val f: A => B => C = s => i => s.toInt+i > 10

val f1: (A, B) => C = f(_)(_)

Similar to the answer by Rex Kerr but easier to read.

type A = String
type B = Int
type C = Boolean

val f: A => B => C = s => i => s.toInt+i > 10

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