我被告知要使用这段有趣的代码,但我的用例要求它比目前可能做的更多。
implicit class Predicate[A](val pred: A => Boolean) {
def apply(x: A) = pred(x)
def &&(that: A => Boolean) = new Predicate[A](x => pred(x) && that(x))
def ||(that: A => Boolean) = new Predicate[A](x => pred(x) || that(x))
def unary_! = new Predicate[A](x => !pred(x))
}
一些用例样板:
type StringFilter = (String) => Boolean
def nameFilter(value: String): StringFilter =
(s: String) => s == value
def lengthFilter(length: Int): StringFilter =
(s: String) => s.length == length
val list = List("Apple", "Orange", "Meat")
val isFruit = nameFilter("Apple") || nameFilter("Orange")
val isShort = lengthFilter(5)
list.filter { (isFruit && isShort) (_) }
到目前为止一切正常。但是说我想做这样的事情:
val nameOption: Option[String]
val lengthOption: Option[Int]
val filters = {
nameOption.map((name) =>
nameFilter(name)
) &&
lengthOption.map((length) =>
lengthFilter(length)
)
}
list.filter { filters (_) }
所以现在我需要&&
一个Option[(A) => Boolean]
如果选项为无,则只需忽略过滤器。
如果我使用类似的东西:
def const(res:Boolean)[A]:A=>Boolean = a => res
implicit def optToFilter[A](optFilter:Option[A => Boolean]):A => Boolean = optFilter match {
case Some(filter) => filter
case None => const(true)[A]
}
我遇到||
的问题,其中一个过滤器设置为true。我可以通过将true更改为false来解决此问题,但&&
存在相同的问题。
我也可以采用这种方法:
implicit def optionalPredicate[A](pred: Option[A => Boolean]): OptionalPredicate[A] = new OptionalPredicate(pred)
class OptionalPredicate[A](val pred: Option[A => Boolean]) {
def apply(x: A) = pred match {
case Some(filter) => filter(x)
case None => trueFilter(x)
}
def &&(that: Option[A => Boolean]) = Some((x: A) =>
pred.getOrElse(trueFilter)(x) && that.getOrElse(trueFilter)(x))
def ||(that: Option[A => Boolean]) = Some((x: A) =>
pred.getOrElse(falseFilter)(x) || that.getOrElse(falseFilter)(x))
}
def trueFilter[A]: A => Boolean = const(res = true)
def falseFilter[A]: A => Boolean = const(res = false)
def const[A](res: Boolean): A => Boolean = a => res
但是当谓词不是选项的子类型时,必须将Predicate转换为OptionalPredicate,这似乎是错误的:
implicit def convertSimpleFilter[A](filter: A => Boolean) = Some(filter)
允许:
ifFruit && isShort
我觉得Optional可以在遵循DRY原则的同时与Predicate无缝结合。
答案 0 :(得分:3)
见下文(UPD)
能够将Option[Filter]
转换为Filter
:
def const(res:Boolean)[A]:A=>Boolean = a => res
implicit def optToFilter[A](optFilter:Option[A => Boolean]):A => Boolean = optFilter match {
case Some(filter) => filter
case None => const(true)[A]
}
然后我们想使用任何可以转换为谓词的类型:
implicit class Predicate[A, P <% A=>Boolean](val pred: P)
(并将pred
的用法更改为(pred:A=>Boolean)
)
def &&[P2:A=>Boolean](that: P2) = new Predicate[A](x => (pred:A=>Boolean)(x) && (that:A=>Boolean)(x))
<强> UPD 强>
Option[A=>Boolean]
将是真正的过滤器类型。
implicit def convertSimpleFilter[A](filter:A=>Boolean)=Some(filter)
implicit class Predicate[A](val pred: Option[A=>Boolean]){
def &&(that:Option[A=>Boolean]) = Some((x:A) => pred.getOrElse(const(true))(x) && that.getOrElse(const(true))(x) )
def ||(that:Option[A=>Boolean]) = Some((x:A) => pred.getOrElse(const(false))(x) || that.getOrElse(const(false))(x) )
}