简体   繁体   English

如何从Scala中的=> b => c得到(a,b)=> c?

[英]How do I get (a, b) => c from a => b => c in Scala?

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: 如何使用签名获得函数g

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

(ie) (即)

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

in terms of f ? f

scala> val f : Int => Int => Int = a => b => a + b
f: (Int) => (Int) => Int = <function1>

scala> Function.uncurried(f)
res0: (Int, Int) => Int = <function2>

Extending retonym's answer, for completeness 为了完整性,扩展了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)

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 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

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)
}

Similar to the answer by Rex Kerr but easier to read. 类似于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(_)(_)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Play for Scala,如何从POST请求中读取JSON正文{a =…,b =…,c…}? - Using Play for Scala, how do I read in JSON body {a=…,b=…,c…} from POST request? 如何从 Spark Scala 程序中得到答案 输入:s='aaabbbccaabb' Output: 3a3b2c2a2b - How to get answer from this Spark Scala program for Input : s= 'aaabbbccaabb' Output : 3a3b2c2a2b 带有子类型的scala中的联合类型:A | B &lt;:A | B | C. - union types in scala with subtyping: A|B <: A|B|C 如何为“ a,b,c” .containsAllOf(“ b”,“ a”)写一个Specs2匹配器? - How to I write a Specs2 matcher for “a,b,c”.containsAllOf(“b”,“a”)? 如果A和B是单子,如何将A [B [C]]转换为B [A [C]]? - How to convert A[B[C]] to B[A[C]] if A and B are monads? def compose [A,B,C](f:B =&gt; C,g:A =&gt; B):A =&gt; C = {f(g(_))}是不是有效的Scala声明? - def compose[A,B,C](f: B => C, g: A => B): A => C = {f(g(_))} is noty valid scala declaration? 给定:aabcdddeabb => 预期:[(a,2),(b,1),(c,1),(d,3),(e,1),(a,1),(b,1)]在 Scala - Given: aabcdddeabb => Expected: [(a,2),(b,1),(c,1),(d,3),(e,1),(a,1),(b,1)] in Scala 用于编写链式比较的scala中的DSL,例如&lt;b &lt;= c - DSL in scala to write chain comparisons such as a < b <= c Scala-“ val a = b = c”的赋值关联性 - Scala - the assignment associativity of “val a = b = c” Scala类型A = B与C,“with”是什么意思 - Scala type A = B with C, what does `with` mean
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM