我有一个我想过滤的列表中的结果。
用户可以为行上的任何属性提供特定的限制(例如,我只想看到x == 1的行)。如果他们没有指定限制,那么当然不使用谓词。当然,最简单的形式是:
list.filter(_.x == 1)
有许多可能的简单谓词,我正在使用代码即时构建一个新的谓词函数,该代码将用户搜索术语(例如选项[int])转换为谓词函数或身份(返回true的函数)。该代码看起来像这样(缩短了,添加了明确的类型以清晰):
case class ResultRow(x: Int, y: Int)
object Main extends App {
// Predicate functions for the specific attributes, along with debug output
val xMatches = (r: ResultRow, i: Int) => { Console println "match x"; r.x == i }
val yMatches = (r: ResultRow, i: Int) => { Console println "match y"; r.y == i }
val Identity = (r : ResultRow) => { Console println "identity"; true }
def makePredicate(a: Option[Int], b: Option[Int]) : ResultRow => Boolean = {
// The Identity entry is just in case all the optional params are None
// (otherwise, flatten would cause reduce to puke)
val expr = List(Some(Identity),
a.map(i => xMatches(_: ResultRow, i)),
b.map(i => yMatches(_: ResultRow, i))
).flatten
// Reduce the function list into a single function.
// Identity only ever appears on the left...
expr.reduceLeft((a, b) => (a, b) match {
case (Identity, f) => f
case (f, f2) => (r: ResultRow) => f(r) && f2(r)
})
}
val rows = List(ResultRow(1, 2), ResultRow(3, 100))
Console println rows.filter(makePredicate(Some(1), None))
Console println rows.filter(makePredicate(None, None))
Console println rows.filter(makePredicate(None, Some(100)))
Console println rows.filter(makePredicate(Some(3), Some(100)))
}
这很好。运行时,它会正确过滤,并且调试输出证明,调用最小数量的功能来适当过滤列表:
match x
match x
List(ResultRow(1,2))
identity
identity
List(ResultRow(1,2), ResultRow(3,100))
match y
match y
List(ResultRow(3,100))
match x
match x
match y
List(ResultRow(3,100))
我实际上对此非常满意。
但是,我不禁认为有一种更有用的方法可以做到这一点(例如,单体和函子和广义总和)...但是我不知道如何使它起作用。
我尝试按照一个scalaz示例,该示例表明我需要创建一个隐式零和半群,但是我无法将零[resultrow => boolean]获得type-check。
您可以使用forall
方法简化代码(无需移动到Scalaz):
def makePredicate(a: Option[Int], b: Option[Int]): ResultRow => Boolean = {
val expr = List(
a.map(i => xMatches(_: ResultRow, i)),
b.map(i => yMatches(_: ResultRow, i))
).flatten
(r: ResultRow) => expr.forall(_(r))
}
请注意,这也消除了将Some(Identity)
包括在列表中的需要。
如果您有很多行,我建议使用zip
与用户输入相匹配xMatches
功能,例如:
val expr = List(a, b) zip List(xMatches, yMatches) flatMap {
case (maybePred, matcher) => maybePred.map(i => matcher(_: ResultRow, i))
}
实际上不再是两行的简洁或可读,而是四到五。
要回答您关于Scalaz的问题,问题是Boolean
有两个可能的单体,而Scalaz并没有为您选择一个,而不是使用Haskell的newtype
包装器标记您的布尔值,以指示哪种单调您想使用(在Scalaz 7中 - 在6中,方法有些不同)。
您指出了您想要哪种 Boolean
的单体,Function1
的单体将启动,而无需做任何事情,您不需要明确定义Identity
零。例如:
import scalaz._, Scalaz._
def makePredicate(a: Option[Int], b: Option[Int]): ResultRow => Boolean =
List(a, b).zip(List(xMatches, yMatches)).flatMap {
case (maybePred, matcher) =>
maybePred.map(i => matcher(_: ResultRow, i).conjunction)
}.suml
在这里,我们刚刚采用了ResultRow => Boolean @@ Conjunction
函数的总和。
我非常喜欢的一种简化是用function1 [a,boolean]的库中的谓词管道简化,该谓词将标准的布尔表达式提升到谓词。这是我的子集:
implicit def toRichPredicate[A](f: Function1[A, Boolean]) = new RichPredicate(f)
def tautology[A] = (x:A)=>true
def falsehood[A] = (x:A)=>false
class RichPredicate[A](f: Function1[A, Boolean]) extends Function1[A, Boolean] {
def apply(v: A) = f(v)
def &&(g: Function1[A, Boolean]): Function1[A, Boolean] = {
(x: A) => f(x) && g(x)
}
def ||(g: Function1[A, Boolean]): Function1[A, Boolean] = {
(x: A) => f(x) || g(x)
}
def unary_! : Function1[A, Boolean] = {
(x: A) => !f(x)
}
}
我发现这可以重复使用。有了这样的东西,您的减少变成
list.flatten.foldLeft(tautology)(&&)
这很简单。它还指向更深的功能优点,因为用重言式和&&清楚地形成了一个单型,因此所有这些都崩溃了,以呼唤Scalaz或Haskell中的一些高阶类型的好处。在这两种情况下,它在其他情况下也可能会变得有些棘手。