简体   繁体   English

使用多重参数 function 作为另一个 function scala 的泛型类型

[英]Using a mutli parameter function as generic type of another function scala

I have the following code我有以下代码

object Test {

  def bar: String => Double = {
    foo[String](_.toDouble)
  }

  def baz: (Double, Double) => Double = {
    foo[(Double, Double)] {
      case (d1, d2) => d1 + d2
    }
  }

  def foo[T](f: T => Double): T => Double = {
    f
  }
}

bar works with no trouble, as expected.正如预期的那样,bar 可以正常工作。 I am trying to get a similar thing working with mutli parameter function as one of the inputs, but this doesn't work, because scala sees the foo[(Double, Double)] as a tuple type rather than as a function parameter. I am trying to get a similar thing working with mutli parameter function as one of the inputs, but this doesn't work, because scala sees the foo[(Double, Double)] as a tuple type rather than as a function parameter. Is there any way to tell scala this is a function parameter, rather than a tuple?有没有办法告诉 scala 这是一个 function 参数,而不是一个元组?

the code: https://scastie.scala-lang.org/sCYyU6ziT3mKOhkBiofAaQ代码: https://scastie.scala-lang.org/sCYyU6ziT3mKOhkBiofAaQ

I think the reason this didn't work for you is that you don't access the two separate arguments to the baz method, which need to be packaged into a tuple before calling foo .我认为这对您不起作用的原因是您没有将两个单独的 arguments 访问到baz方法,需要在调用foo之前将其打包成一个元组。 Try this definition instead:试试这个定义:

object Test {
  def bar: String => Double = {
    foo[String](_.toDouble)
  }
  def baz: (Double, Double) => Double = {(p1, p2) =>
    foo[(Double, Double)] {
      case(d1, d2) => d1 + d2
    }((p1, p2))
  }
  def foo[T](f: T => Double): T => Double = f
}

If I understand your intent correctly, this would be used like this (in the Scala REPL ):如果我正确理解您的意图,这将像这样使用(在Scala REPL中):

scala> Test.bar("5.0")
val res0: Double = 5.0
                                                                                                                                    
scala> Test.baz(3.0, 5.0)
val res1: Double = 8.0

However, this seems a little muddled.然而,这似乎有点混乱。 If bar and baz store method references, then they should be declared val , instead of def :如果barbaz存储方法引用,那么它们应该被声明为val ,而不是def

object Test {
  val bar: String => Double = {
    foo[String](_.toDouble)
  }
  val baz: (Double, Double) => Double = {(p1, p2) =>
    foo[(Double, Double)] {
      case(d1, d2) => d1 + d2
    }((p1, p2))
  }
  def foo[T](f: T => Double): T => Double = f
}

Which has the same usage syntax and produces the same output:它具有相同的使用语法并产生相同的 output:

scala> Test.bar("5.0")
val res2: Double = 5.0
                                                                                                                                    
scala> Test.baz(3.0, 5.0)
val res3: Double = 8.0

But even that is still fairly clumsy.但即便如此,仍然相当笨拙。 bar is a reference to a function that takes a String and returns a Double , while baz is a reference to a function that takes two Double s and returns a Double . bar是对采用String并返回Double的 function 的引用,而baz是对采用两个Double并返回Double的 function 的引用。 So why not just make bar and baz methods in their own right, instead of method references?那么为什么不单独创建barbaz方法,而不是方法引用呢? This would produce:这将产生:

object Test {
  def bar(s: String): Double = foo[String](_.toDouble)(s)

  def baz(p1: Double, p2: Double): Double = {
    foo[(Double, Double)] {
      case(d1, d2) => d1 + d2
    }((p1, p2))
  }

  def foo[T](f: T => Double): T => Double = f
}

which again is used in the same way:再次以相同的方式使用:

scala> Test.bar("5.0")
val res4: Double = 5.0
                                                                                                                                    
scala> Test.baz(3.0, 5.0)
val res5: Double = 8.0

And, of course, foo itself is redundant, since it is identical to its argument.而且,当然, foo本身是多余的,因为它与它的论点相同。 So this could all be written far more simply as just:所以这一切都可以更简单地写成:

object Test {
  def bar(s: String): Double = s.toDouble

  def baz(p1: Double, p2: Double): Double = p1 + p2
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM