我只是在寻找更好的Scala做事情的方法,所以问新手问题。例如:
我希望能够做下面这样的事情:
给定a、b、c为“布尔值”:
if (((a nand b) nand c) != (a nand (b nand c))) printf("NAND is not associative")我会循环使用可能的a,b,c布尔值。我是通过以下方式做到的:
for (i <- 0 to 7) {
val (a,b,c) = (new MyBoolean((i & 4) >> 2 == 1),
new MyBoolean((i & 2) >> 1 == 1),
new MyBoolean((i & 1) == 1))
printf("%d (%s,%s,%s)\n",i,a,b,c)
if (((a nand b) nand c) != (a nand (b nand c))) printf("NAND\n")
}我想我可以把它简化为:
val (a,b,c) = (new MyBoolean(i & 4 != 0),
new MyBoolean(i & 2 != 0),
new MyBoolean(i & 1 != 0))我的MyBoolean类看起来像:
class MyBoolean(val p: Boolean) {
def and(q: MyBoolean): MyBoolean = new MyBoolean(p && q.p)
override def toString: String = p.toString
override def equals (o : Any): Boolean = o match {
case m : MyBoolean => p == m.p
case _ => false
}
def and(q: Boolean): MyBoolean = new MyBoolean(p && q)
def or(q: Boolean): MyBoolean = new MyBoolean(p || q)
def or(q: MyBoolean): MyBoolean = or(q.p)
def negate: MyBoolean = new MyBoolean(!p)
def nand(q : Boolean): MyBoolean = new MyBoolean(!(p && q))
def nand(q : MyBoolean): MyBoolean = nand(q.p)
def nor(q : Boolean): MyBoolean = new MyBoolean(!(p || q))
def nor(q : MyBoolean): MyBoolean = nor(q.p)
def xor(q : Boolean): MyBoolean = new MyBoolean((p || q) && !(p && q))
def xor(q : MyBoolean): MyBoolean = xor(q.p)
def implies(q : Boolean): MyBoolean = new MyBoolean(!(p && !q))
def implies(q : MyBoolean): MyBoolean = implies(q.p)
def equiv(q : Boolean): MyBoolean = new MyBoolean(p == q)
def equiv(q : MyBoolean): MyBoolean = equiv(q.p)
}这是合理的吗,还是有更好的方法呢?:)
我没有机会尝试像这样的事情:
def nand(p : Boolean, q : Boolean): Boolean = !(p && q)我不能从那里去:
(a nand (b nand c))正如我所希望的(与希望相反)。:)我真的不想引入新的类型,如果只有nand,nor等就好了。使用布尔值。
我想我的main也呼吁减少冗余,我在几行代码中使用了不同的函数……但唯一变化的是函数名。
def main(args: Array[String]): Unit = {
for (i <- 0 to 7) {
val (a,b,c) = (new MyBoolean((i & 4) != 0),
new MyBoolean((i & 2) != 0),
new MyBoolean((i & 1) != 0))
printf("%d (%s,%s,%s)\n",i,a,b,c)
if (((a nand b) nand c) != (a nand (b nand c))) printf("NAND\n")
if (((a implies b) implies c) != (a implies (b implies c))) printf("IMPLIES\n")
if (((a nor b) nor c) != (a nor (b nor c))) printf("NOR\n")
if (((a xor b) xor c) != (a xor (b xor c))) printf("XOR\n")
if (((a equiv b) equiv c) != (a equiv (b equiv c))) printf("EQUIV\n")
}
}-Jay
发布于 2010-08-29 12:13:23
您应该为此使用隐式转换,如下所示:
class MyBoolean {/*...*/}
implicit def decorateBoolean(x:Boolean) = new MyBoolean(x)
for (i <- 0 to 7) {
val (a,b,c) = ((i & 4) != 0,
(i & 2) != 0,
(i & 1) != 0)
printf("%d (%s,%s,%s)\n",i,a,b,c)
if (((a nand b) nand c) != (a nand (b nand c))) printf("NAND\n")
}现在,您已经从源代码中移除了对象构造的语法负担。您只需希望JVM在JIT您的代码时适当地优化对象构造。
发布于 2010-08-29 18:21:01
作为Ken Bloom,我建议使用隐式转换来节省所有的输入。在另一个方向上进行转换(MyBoolean,->,Boolean)也很方便。
此外,当你只有两个不同的值时,创建一个新的实例是非常非常浪费的。如果对这些实例使用两个对象,则不再需要对原始Boolean类型的任何引用。
以下是我将如何编写它:
sealed trait MyBoolean {
import MyBoolean._
def and(that: MyBoolean): MyBoolean = (this, that) match {
case (TRUE,TRUE) => TRUE
case _ => FALSE
}
def or(that: MyBoolean): MyBoolean = (this, that) match {
case (FALSE, FALSE) => FALSE
case _ => TRUE
}
def negate: MyBoolean = this != TRUE
def nand(that: MyBoolean): MyBoolean = (this and that).negate
def nor(that: MyBoolean): MyBoolean = (this or that).negate
def xor(that: MyBoolean): MyBoolean = this != that
def implies(that: MyBoolean): MyBoolean = (this and that.negate).negate
def equiv(that: MyBoolean): MyBoolean = this == that
}
case object TRUE extends MyBoolean
case object FALSE extends MyBoolean
object MyBoolean {
implicit def bool2MyBool(p:Boolean):MyBoolean = MyBoolean(p)
implicit def myBool2bool(m:MyBoolean):Boolean = m == TRUE
def apply(p:Boolean):MyBoolean = if (p) TRUE else FALSE
}请注意,您不需要equals (因为我们现在可以依赖对象标识)和toString (因为这已经为case对象和类实现了)。
考虑到肯的话,编辑实现:
sealed trait MyBoolean {
def and(that: MyBoolean): MyBoolean
def or(that: MyBoolean): MyBoolean
def negate: MyBoolean
def equiv(that: MyBoolean): MyBoolean = if (this == that) TRUE else FALSE
def nand(that: MyBoolean): MyBoolean = (this and that).negate
def nor(that: MyBoolean): MyBoolean = (this or that).negate
def xor(that: MyBoolean): MyBoolean = (this equiv that).negate
def implies(that: MyBoolean): MyBoolean = (this and that.negate).negate
}
case object TRUE extends MyBoolean {
def negate = FALSE
def and(that:MyBoolean) = that
def or(that:MyBoolean) = this
}
case object FALSE extends MyBoolean {
def negate = TRUE
def and(that:MyBoolean) = this
def or(that:MyBoolean) = that
}
object MyBoolean {
implicit def bool2MyBool(p:Boolean):MyBoolean = MyBoolean(p)
implicit def myBool2bool(m:MyBoolean):Boolean = m == TRUE
def apply(p:Boolean):MyBoolean = if (p) TRUE else FALSE
}发布于 2010-08-30 01:25:06
这是对Landei的回答的改进,基于Ruby如何做到这一点:
sealed trait MyBoolean {
val toBoolean:Boolean
def and(that: MyBoolean): MyBoolean
def or(that: MyBoolean): MyBoolean
def negate: MyBoolean
def nand(that: MyBoolean): MyBoolean
def nor(that: MyBoolean): MyBoolean
def xor(that: MyBoolean): MyBoolean
def implies(that: MyBoolean): MyBoolean
def equiv(that: MyBoolean): MyBoolean
}
case object TRUE extends MyBoolean{
override val toBoolean = true
override def and(that:MyBoolean) = that
override def or(that:MyBoolean) = TRUE
override def negate: MyBoolean = FALSE
override def nand(that:MyBoolean) = that.negate
override def nor(that:MyBoolean) = FALSE
override def xor(that:MyBoolean) = that.negate
override def implies(that:MyBoolean) = that
override def equiv(that:MyBoolean) = that
}
case object FALSE extends MyBoolean{
override val toBoolean = false
override def and(that:MyBoolean) = FALSE
override def or(that:MyBoolean) = that
override def negate: MyBoolean = TRUE
override def nand(that:MyBoolean) = TRUE
override def nor(that:MyBoolean) = that.negate
override def xor(that:MyBoolean) = that
override def implies(that:MyBoolean) = TRUE
override def equiv(that:MyBoolean) = that.negate
}
object MyBoolean {
implicit def bool2MyBool(p:Boolean):MyBoolean = MyBoolean(p)
implicit def myBool2bool(m:MyBoolean):Boolean = m.toBoolean
def apply(p:Boolean):MyBoolean = if (p) TRUE else FALSE
}https://stackoverflow.com/questions/3593453
复制相似问题