How to compare Scala function values for equality

前端 未结 2 1601
面向向阳花
面向向阳花 2020-12-06 17:21

How can you compare two Scala function values for equality. The use case is that I have a list of functions where the list can contain duplicates and I only want to execute

相关标签:
2条回答
  • 2020-12-06 17:48

    Short answer: It's not possible.

    Longer answer: You could have some kind of function factory that ensures that "identical" functions are acutally the same object. Depending on the architecture of your application, that might not be feasible though.

    0 讨论(0)
  • 2020-12-06 17:57

    I want to extent a bit on Kim's answer and give an example of how to achieve a limited comparability of function values.

    If you have some kind of descriptive definition of your function, it is possible to check for equality on this description. For example, you can define a class (not an oo class) of simple arithmetic functions in the following way:

    sealed trait ArthFun extends (Double => Double)
    case class Mult(x: Double) extends ArthFun {def apply(y: Double) = x * y}
    case class Add(x: Double) extends ArthFun {def apply(y: Double) = x + y}
    

    With this setup, where an ArthFun is defined by its class and members, you can check for equality of values of the ArthFun type simply by object equality as defined by the case class.

    scala> trait ArthFun extends (Double => Double)
    defined trait ArthFun
    
    scala> case class Mult(y: Double) extends ArthFun { def apply(x: Double) = x * y; override def toString = "*" + y}
    defined class Mult
    
    scala> case class Add(y: Double) extends ArthFun { def apply(x: Double) = x + y; override def toString = "+" + y }
    defined class Add
    
    scala> Seq(Mult(5),Mult(4),Add(4),Add(3),Mult(5)).distinct
    res4: Seq[Product with ArthFun with Serializable] = List(*5.0, *4.0, +4.0, +3.0)
    
    0 讨论(0)
提交回复
热议问题