我正在Scala为大学实验室实施标志分析。
为此,我需要对Pos
,Neg
,Zero
等抽象值进行算术运算,
NonPos
,NonNeg
,...所以我必须声明方法+
,-
,*
,/
等...
那些抽象的价值。我实际上不需要定义所有内容
成对,但可以定义一个核心'对Pos
,Neg
和Zero
的操作,
然后使用上限来定义,例如:
NonPos + Pos = leastUpperBound(Zero + Pos, Neg + Pos)
例如leastUpperBound(Zero, Neg) = NonPos
在Scala中,我使用case对象来表示值,并且有一个
每个人都有leastUpperBound()
个方法。但我还是有一些代码
重复我没有设法摆脱,例如我定义:
case object NonNeg extends Sign {
def +(other: Sign) = leastUpperBound(Zero + other, Pos + other)
def -(other: Sign) = ...
def * = ...
...
}
我必须做同样的事情:
case object NonPos extends Sign {
def +(other: Sign) = leastUpperBound(Zero + other, Neg + other)
...
}
再次:
case object NonZero extends Sign {
def +(other: Sign) = leastUpperBound(Neg + other, Neg + other)
...
}
我想知道是否有可能拥有某种类型的工厂"这样我就可以 说出一点精神:
case object NonNeg extends UpperBoundSign[Pos, Zero]
我的直觉是,Pos
和Zero
object
是不可能的,
但我对Scala并不熟悉,所以我可能会忘记一些特征或模式
请允许我这样做。
有没有人知道要删除这种重复? 也许2.10中的Scala宏可能适合这个问题吗?
我希望问题很清楚, 谢谢。
编辑:感谢@ cmbaxter的回答以及我的一些重构,我想出了一个我喜欢的解决方案。如果有人有兴趣看到它,可以在那里找到:https://gist.github.com/Ricordel/5553405。答案 0 :(得分:5)
我认为你可能会混淆类型标识符和类的实例。我相信,为了获得您想要的功能,您需要将UpperBoundSign
定义为带有两个构造函数args的抽象类,而不是具有两个类型标识符槽的泛型类型。这是一个过于简化的解决方案选项,可以满足您的需求。如果这完全不是你想要的,我道歉:
trait Sign{
def +(other: Sign):Sign
}
abstract class UpperBoundSign(pos:Sign, neg:Sign) extends Sign{
def leastUpperBound(pos:Sign, neg:Sign):Sign
def +(other: Sign) = leastUpperBound(pos + other, neg + other)
}
case object Pos extends Sign{
def +(other:Sign) = ...
}
case object Neg extends Sign{
def +(other:Sign) = ...
}
case object NonNeg extends UpperBoundSign(Pos, Neg){
def leastUpperBound(pos:Sign, neg:Sign) = ...
}
答案 1 :(得分:1)
object Sign {
case object Pos extends Sign
case object Neg extends Sign
case object Zero extends Sign
case object Undefined extends Sign
case object NonPos extends SignSet(Set(Neg, Zero)) {
override def toString = "NonPos"
}
case object NonNeg extends SignSet(Set(Pos, Zero)) {
override def toString = "NonNeg"
}
case object NonZero extends SignSet(Set(Pos, Neg)) {
override def toString = "NonZero"
}
case object AnySign extends SignSet(Set(Pos, Neg, Zero)) {
override def toString = "AnySign"
}
private val signs = List(Pos, Neg, Zero, Undefined, NonPos, NonNeg, NonZero, AnySign)
private def calc(op: Symbol, s1: Sign, s2: Sign): Sign = {
val sign = _calc(op, s1, s2)
signs.find(_ == sign).getOrElse(sign)
}
private def _calc(op: Symbol, s1: Sign, s2: Sign): Sign = (op, s1, s2) match {
case (op, set: SignSet, sign) => set.flatMap(s => _calc(op, s, sign))
case (op, sign, set: SignSet) => set.flatMap(s => _calc(op, sign, s))
case (_, Undefined, _) => Undefined
case (_, _, Undefined) => Undefined
case ('+, x, y) if x == y => x
case ('+, x, Zero) => x
case ('+, Zero, x) => x
case ('+, Pos, Neg) => SignSet(Pos, Neg, Zero)
case ('+, Neg, Pos) => SignSet(Pos, Neg, Zero)
case ('-, x, Neg) => _calc('+, x, Pos)
case ('-, x, Pos) => _calc('+, x, Neg)
case ('-, x, Zero) => x
case ('*, Zero, _) => Zero
case ('*, Pos, x) => x
case ('*, Neg, Pos) => Neg
case ('*, Neg, Neg) => Pos
case ('*, Neg, Zero) => Zero
case ('/, _, Zero) => Undefined
case ('/, x, y) => _calc('*, x, y)
}
}
sealed trait Sign {
import Sign.calc
def +(other: Sign) = calc('+, this, other)
def -(other: Sign) = calc('-, this, other)
def *(other: Sign) = calc('*, this, other)
def /(other: Sign) = calc('/, this, other)
def flatten: Sign = this
def |(other: Sign): Sign = other match {
case sign if sign == this => this
case SignSet(signs) => SignSet(signs + this)
case sign => SignSet(this, sign)
}
}
object SignSet {
def apply(signs: Set[Sign]) = new SignSet(signs)
def apply(signs: Sign*) = new SignSet(signs.toSet)
def unapply(set: SignSet) = Some(set.signs)
}
class SignSet(val signs: Set[Sign]) extends Sign {
def flatMap(f: Sign => Sign) = SignSet(signs.map(f)).flatten
override def flatten = signs.map(_.flatten).reduce(_ | _)
override def |(other: Sign) = other match {
case SignSet(otherSigns) => SignSet(otherSigns | signs)
case sign => SignSet(signs + sign)
}
override def toString = signs.mkString("SignSet(", ", ", ")")
def equals(other: SignSet) = signs == other.signs
override def equals(other: Any) = other match {
case set: SignSet => equals(set)
case _ => false
}
}
import Sign._
println(Pos / NonPos)
println(Pos + Neg)
println(NonZero * Zero)
println(NonZero / NonPos)
println(NonZero - NonZero)
println(NonZero + Zero)
答案 2 :(得分:0)
你可以用集合论来尝试。
abstract class Sign(name: String) {
def contains(sign: Sign) = sign eq this
def +(other: Sign) = findSign(Union(this, other))
def -(other: Sign) = findSign(Difference(this, other))
def equals(other: Sign) = (contains(Pos), contains(Neg), contains(Zero)) == (other.contains(Pos), other.contains(Neg), other.contains(Zero))
override def equals(other: Any) = {
if(other.isInstanceOf[Sign]) equals(other.asInstanceOf[Sign])
else false
}
override def toString = name
}
case class Union(sign1: Sign, sign2: Sign, name: String = "Union") extends Sign(name) {
override def contains(sign: Sign) = sign1.contains(sign) || sign2.contains(sign)
}
case class Intersection(sign1: Sign, sign2: Sign, name: String = "Intersection") extends Sign(name) {
override def contains(sign: Sign) = (sign1.contains(sign) || sign2.contains(sign)) && !(sign1.contains(sign) && sign2.contains(sign))
}
case class Difference(sign1: Sign, sign2: Sign, name: String = "Difference") extends Sign(name) {
override def contains(sign: Sign) = sign1.contains(sign) && !sign2.contains(sign)
}
case class Negation(sign: Sign, name: String = "Negation") extends Sign(name) {
override def contains(s: Sign) = !sign.contains(s)
}
case object Zero extends Sign("Zero")
case object Pos extends Sign("Pos")
case object Neg extends Sign("Neg")
val NonPos = Negation(Pos, "NonPos")
val NonNeg = Negation(Neg, "NonNeg")
val NonZero = Negation(Zero, "NonZero")
val AnySign = Union(NonZero, Zero, "AnySign")
val NoSign = Negation(AnySign, "NoSign")
val signs = List(Zero, Pos, Neg, NonPos, NonNeg, NonZero, AnySign, NoSign)
def findSign(sign: Sign) = signs.find(_ == sign).get
println(Pos + Neg)
println(NonNeg - Zero)
println(NonZero + Zero)
println(Pos + Neg + Zero)
println(NonPos - Neg - Zero)