title: Building a minimal predicate function by composing Option[predicate] functions in Scala (possibly with scalaz)
body: <p>I have results in a list that I wish to filter.</p>
  
  <p>The user can supply a specific limit to any of the attributes on a row (e.g., I only want to see the rows where x == 1). If they specify no limit, then of course that predicate is not used. The simplest form of this, of course, is:</p>
  
  <pre><code>list.filter(_.x == 1)
  </code></pre>
  
  <p>There are many possible simple predicates, and I am building a new predicate function on the fly with code that converts the user search terms (e.g. Option[Int]) into predicate functions or Identity (a function that returns true). The code looks like this (shortened, with explicit types added for clarity):</p>
  
  <pre><code>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) =&gt; { Console println "match x"; r.x == i }
    val yMatches = (r: ResultRow, i: Int) =&gt; { Console println "match y"; r.y == i }
    val Identity = (r : ResultRow) =&gt; { Console println "identity"; true }
  
    def makePredicate(a: Option[Int], b: Option[Int]) : ResultRow =&gt; 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 =&gt; xMatches(_: ResultRow, i)),
                      b.map(i =&gt; yMatches(_: ResultRow, i))
                     ).flatten
  
      // Reduce the function list into a single function. 
      // Identity only ever appears on the left...
      expr.reduceLeft((a, b) =&gt; (a, b) match {
        case (Identity, f) =&gt; f
        case (f, f2) =&gt; (r: ResultRow) =&gt; f(r) &amp;&amp; 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)))
  }
  </code></pre>
  
  <p>This works perfectly. When run, it filters properly, and the debug output proves that the minimal number of functions are called to appropriately filter the list:</p>
  
  <pre><code>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))
  </code></pre>
  
  <p>I am actually very happy with how well this came out.</p>
  
  <p>But, I can't help but think there is a more functional way to do it (e.g. Monoids and Functors and generalized sum)...but I cannot figure out how to make it work.</p>
  
  <p>I tried following a scalaz example that indicated I needed to create an implicit zero and semigroup, but I could not get Zero[ResultRow => Boolean] to type-check.</p>