scala中的DSL用于编写链比较,如<;b<;=C

scala中的DSL用于编写链比较,如<;b<;=C,scala,comparison,Scala,Comparison,我想用Scala编写以下内容: def method(a: Float, b: Float, c: Float) = { if( a < b <= c) { ... } } 有什么方法可以使用标准技巧删除此函数?如果使用具有方法的标准类型,则在没有显式换行的情况下,这是不可能的。如果使用具有方法的标准类型,则在没有显式换行的情况下,这是不可能的。是的,可以使用隐式运算符和自定义运算符模拟此操作。 下面是一个简短的例子: implicit class FloatWra

我想用Scala编写以下内容:

def method(a: Float, b: Float, c: Float) = {
  if( a < b <= c) {
    ...
  }
}

有什么方法可以使用标准技巧删除此函数?

如果使用具有方法的标准类型,则在没有显式换行的情况下,这是不可能的。如果使用具有方法的标准类型,则在没有显式换行的情况下,这是不可能的。是的,可以使用隐式运算符和自定义运算符模拟此操作。 下面是一个简短的例子:

implicit class FloatWrapper(n:Float) {
  def :<(that:Float) = ResultAndLastItem(n<that, that)
  def :>(that:Float) = ResultAndLastItem(n>that, that)
  def :=(that:Float) = ResultAndLastItem(n==that, that)
  // insert more comparison operations here
}

case class ResultAndLastItem(result:Boolean, lastItem:Float) extends FloatWrapper(lastItem) {
  // insert boolean operations here
  // boolean operations should return another instance of ResultAndLastItem (?)
}

implicit def toBoolean(r:ResultAndLastItem):Boolean = r.result
隐式类FloatWrapper(n:Float){
戴夫:那,那)
def:=(that:Float)=ResultAndLastItem(n==that,that)
//在此处插入更多比较操作
}
案例类ResultAndLastItem(结果:Boolean,lastItem:Float)扩展了FloatWrapper(lastItem){
//在此处插入布尔运算
//布尔运算应返回ResultAndLastItem(?)的另一个实例
}
隐式def-toBoolean(r:ResultAndLastItem):Boolean=r.result
以下是REPL中的一个示例用法:

Welcome to Scala version 2.10.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_45).
Type in expressions to have them evaluated.
Type :help for more information.

scala> :paste
// Entering paste mode (ctrl-D to finish)

implicit class FloatWrapper(n:Float) {
  def :<(that:Float) = ResultAndLastItem(n<that, that)
  def :>(that:Float) = ResultAndLastItem(n>that, that)
  def :=(that:Float) = ResultAndLastItem(n==that, that)
  // insert more comparison operations here
}

case class ResultAndLastItem(result:Boolean, lastItem:Float) extends FloatWrapper(lastItem) {
  // insert boolean operations here
  // boolean operations should return another instance of ResultAndLastItem (?)
}

implicit def toBoolean(r:ResultAndLastItem):Boolean = r.result

// Exiting paste mode, now interpreting.

warning: there were 1 feature warnings; re-run with -feature for details
defined class FloatWrapper
defined class ResultAndLastItem
toBoolean: (r: ResultAndLastItem)Boolean

scala> if(2 :< 3 :< 4) "yay" else "nope"
res0: String = yay

scala> if(2 :< 3 :< 3) "nope" else "yay"
res1: String = yay
欢迎使用Scala 2.10.0版(Java HotSpot(TM)64位服务器虚拟机,Java 1.6.045)。
键入要计算的表达式。
键入:有关详细信息的帮助。
scala>:粘贴
//进入粘贴模式(按ctrl-D键完成)
隐式类FloatWrapper(n:Float){
戴夫:那,那)
def:=(that:Float)=ResultAndLastItem(n==that,that)
//在此处插入更多比较操作
}
案例类ResultAndLastItem(结果:Boolean,lastItem:Float)扩展了FloatWrapper(lastItem){
//在此处插入布尔运算
//布尔运算应返回ResultAndLastItem(?)的另一个实例
}
隐式def-toBoolean(r:ResultAndLastItem):Boolean=r.result
//正在退出粘贴模式,现在正在解释。
警告:有1个功能警告;有关详细信息,请使用-功能重新运行
定义类FloatWrapper
定义的类ResultAndLastItem
toBoolean:(r:ResultAndLastItem)布尔值
scala>如果(2:<3:<4)“是”或“否”
res0:String=yay
scala>如果(2:<3:<3)“不”或“是”
res1:String=yay
备注:


  • 您可以轻松地添加更多比较运算符,例如
    :是的,您可以使用隐式运算符和自定义运算符模拟此操作。
    下面是一个简短的例子:

    implicit class FloatWrapper(n:Float) {
      def :<(that:Float) = ResultAndLastItem(n<that, that)
      def :>(that:Float) = ResultAndLastItem(n>that, that)
      def :=(that:Float) = ResultAndLastItem(n==that, that)
      // insert more comparison operations here
    }
    
    case class ResultAndLastItem(result:Boolean, lastItem:Float) extends FloatWrapper(lastItem) {
      // insert boolean operations here
      // boolean operations should return another instance of ResultAndLastItem (?)
    }
    
    implicit def toBoolean(r:ResultAndLastItem):Boolean = r.result
    
    隐式类FloatWrapper(n:Float){
    戴夫:那,那)
    def:=(that:Float)=ResultAndLastItem(n==that,that)
    //在此处插入更多比较操作
    }
    案例类ResultAndLastItem(结果:Boolean,lastItem:Float)扩展了FloatWrapper(lastItem){
    //在此处插入布尔运算
    //布尔运算应返回ResultAndLastItem(?)的另一个实例
    }
    隐式def-toBoolean(r:ResultAndLastItem):Boolean=r.result
    
    以下是REPL中的一个示例用法:

    Welcome to Scala version 2.10.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_45).
    Type in expressions to have them evaluated.
    Type :help for more information.
    
    scala> :paste
    // Entering paste mode (ctrl-D to finish)
    
    implicit class FloatWrapper(n:Float) {
      def :<(that:Float) = ResultAndLastItem(n<that, that)
      def :>(that:Float) = ResultAndLastItem(n>that, that)
      def :=(that:Float) = ResultAndLastItem(n==that, that)
      // insert more comparison operations here
    }
    
    case class ResultAndLastItem(result:Boolean, lastItem:Float) extends FloatWrapper(lastItem) {
      // insert boolean operations here
      // boolean operations should return another instance of ResultAndLastItem (?)
    }
    
    implicit def toBoolean(r:ResultAndLastItem):Boolean = r.result
    
    // Exiting paste mode, now interpreting.
    
    warning: there were 1 feature warnings; re-run with -feature for details
    defined class FloatWrapper
    defined class ResultAndLastItem
    toBoolean: (r: ResultAndLastItem)Boolean
    
    scala> if(2 :< 3 :< 4) "yay" else "nope"
    res0: String = yay
    
    scala> if(2 :< 3 :< 3) "nope" else "yay"
    res1: String = yay
    
    欢迎使用Scala 2.10.0版(Java HotSpot(TM)64位服务器虚拟机,Java 1.6.045)。
    键入要计算的表达式。
    键入:有关详细信息的帮助。
    scala>:粘贴
    //进入粘贴模式(按ctrl-D键完成)
    隐式类FloatWrapper(n:Float){
    戴夫:那,那)
    def:=(that:Float)=ResultAndLastItem(n==that,that)
    //在此处插入更多比较操作
    }
    案例类ResultAndLastItem(结果:Boolean,lastItem:Float)扩展了FloatWrapper(lastItem){
    //在此处插入布尔运算
    //布尔运算应返回ResultAndLastItem(?)的另一个实例
    }
    隐式def-toBoolean(r:ResultAndLastItem):Boolean=r.result
    //正在退出粘贴模式,现在正在解释。
    警告:有1个功能警告;有关详细信息,请使用-功能重新运行
    定义类FloatWrapper
    定义的类ResultAndLastItem
    toBoolean:(r:ResultAndLastItem)布尔值
    scala>如果(2:<3:<4)“是”或“否”
    res0:String=yay
    scala>如果(2:<3:<3)“不”或“是”
    res1:String=yay
    
    备注:

    • 您可以轻松添加更多比较运算符,例如
      :的可能重复