title: faster min and max of different array components with CouchDb map/reduce?
body: <p>I have a CouchDB database with a view whose values are paired numbers of the form [x,y].  For documents with the same key, I need (simultaneously) to compute the minimum of x and the maximum of y.  The database I am working with contains about 50000 documents.  Building the view takes several hours, which seems somewhat excessive. (The keys are themselves length-three arrays.) I show the map and reduce functions below, but the basic question is: how can I speed up this process? </p>
  
  <p>Note that the builtin functions won't work because the values have to be numbers, not length-two arrays. It is possible that I could make two different views (one for min(x) and one for max(y)), but it is unclear to me how to combine them to get both results simultaneously.</p>
  
  <p>My current map function looks basically like</p>
  
  <pre><code>function(doc) {
    emit ([doc.a, doc.b, doc.c], [doc.x, doc.y])
  }
  </code></pre>
  
  <p>and my reduce function looks like</p>
  
  <pre><code>function(keys, values) {
    var x = null;
    var y = null;
    for (i = 0; i &lt; values.length; i++) {
      if (values[i][0] == null) break;
      if (values[i][1] == null) break;
      if (x == null) x = values[i][0];
      if (y == null) y = values[i][1];
      if (values[i][0] &lt; x) x = values[i][0];
      if (values[i][1] &gt; y) y = values[i][1];
    }
    emit([x, y]);
  }
  </code></pre>