// These methodss are taken from the Higher-Order Javascript page, found here:
// http://interglacial.com/hoj/hoj.html
// They are used here by permission of the author, Sean M. Burke

 Array.prototype.map = function(f) {
   if(!f.apply) { var propname = f; f = function(_) { return _[propname] } }
   var out = [];
   for(var i = 0; i < this.length; i++) {
     out.push( f( this[i], this, i) );
   }
   return out;  
 };

 Array.prototype.mapc = function(f) {
   if(!f.apply) { var propname = f; f = function(_) { return _[propname] } }
   var out = [];
   var gotten;
   for(var i = 0; i < this.length; i++) {
     gotten = f( this[i], this, i);
     if( gotten != undefined )   out = out.concat( gotten );
   }
   return out;  
 };

 Array.prototype.grep = function(f) {
   if(!f.apply) { var propname = f; f = function(_) { return _[propname] } }
   var out = [];
   for(var i = 0; i < this.length; i++) {
     if( f( this[i], this, i)) out.push(this[i]);
   }
   return out;  
 };

 Array.prototype.foreach = function(f) {
   if(!f.apply) { var propname = f; f = function(_,x,i) { x[i] = _[propname] }
   }
   for(var i = 0; i < this.length; i++) {
     f( this[i], this, i );
   }
   return;
 };

function map (f, inarray) {
   var out = [];
   for(var i = 0; i < inarray.length; i++) {
     out.push( f(inarray[i]) )
   }
   return out;
}

// Additions in the same vein

 Array.prototype.firstMatch = function(f) {
   if(!f.apply) { var propname = f; f = function(_) { return _[propname] } }
   for(var i = 0; i < this.length; i++) {
     if( f( this[i], this, i)) return this[i];
   }
   return undefined;  
 };

 Array.prototype.doToFirstMatch = function(f, fAction) {
   if(!f.apply) { var propname = f; f = function(_) { return _[propname] } }
   for(var i = 0; i < this.length; i++) {
     if( f( this[i], this, i)) { return fAction(this[i]); }
   }
   return undefined;  
 };

 Array.prototype.doToMatches = function(f, fAction) {
   if(!f.apply) { var propname = f; f = function(_) { return _[propname] } }
   for(var i = 0; i < this.length; i++) {
     if( f( this[i], this, i)) fAction(this[i]);
   }
   return undefined;  
 };

function foreach (f, inarray) {
   for(var i = 0; i < inarray.length; i++) {
     f(inarray[i]);
   }
}

