How to apply a function to a tuple?

后端 未结 5 1924
迷失自我
迷失自我 2020-11-29 22:56

This should be an easy one. How do I apply a function to a tuple in Scala? Viz:

scala> def f (i : Int, j : Int) = i + j
f: (Int,Int)Int

scala> val p = (3,4)
p:          


        
5条回答
  •  悲&欢浪女
    2020-11-29 23:21

    In Scala 2.8 and newer:

    scala> def f (i : Int, j : Int) = i + j
    f: (i: Int,j: Int)Int
    
    // Note the underscore after the f
    scala> val ff = f _
    ff: (Int, Int) => Int = 
    
    scala> val fft = ff.tupled
    fft: ((Int, Int)) => Int = 
    

    In Scala 2.7:

    scala> def f (i : Int, j : Int) = i + j
    f: (Int,Int)Int
    
    // Note the underscore after the f
    scala> val ff = f _
    ff: (Int, Int) => Int = 
    
    scala> val fft = Function.tupled(ff)
    fft: ((Int, Int)) => Int = 
    

提交回复
热议问题