JavaScript
// Return an array that holds the names of the enumerable properties of o function getPropertyNames(/* object */o) { var r = []; for(name in o) r.push(name); return r; } // Copy the enumerable properties of the object from to the object to. // If to is null, a new object is created. The function returns to or the // newly created object. function copyProperties(/* object */ from, /* optional object */ to) { if (!to) to = {}; for(p in from) to[p] = from[p]; return to; } // Copy the enumerable properties of the object from to the object to, // but only the ones that are not already defined by to. // This is useful, for example, when from contains default values that // we want to use if they are not already defined in to. function copyUndefinedProperties(/* object */ from, /* object */ to) { for(p in from) { if (!p in to) to[p] = from[p]; } }
// Pass each element of the array a to the specified predicate function. // Return an array that holds the elements for which the predicate // returned true function filterArray(/* array */ a, /* boolean function */ predicate) { var results = []; var length = a.length; // In case predicate changes the length! for(var i = 0; i < length; i++) { var element = a[i]; if (predicate(element)) results.push(element); } return results; } // Return the array of values that result when each of the elements // of the array a are passed to the function f function mapArray(/* array */a, /* function */ f) { var r = []; // to hold the results var length = a.length; // In case f changes the length! for(var i = 0; i < length; i++) r[i] = f(a[i]); return r; }
// Return a standalone function that invokes the function f as a method of // the object o. This is useful when you need to pass a method to a function. // If you don't bind it to its object, the association will be lost and // the method you passed will be invoked as a regular function. function bindMethod(/* object */ o, /* function */ f) { return function() { return f.apply(o, arguments) } } // Return a function that invokes the function f with the // specified arguments and also any additional arguments that are // passed to the returned function. (This is sometimes called "currying".) function bindArguments(/* function */ f /*, initial arguments... */) { var boundArgs = arguments; return function() { // Build up an array of arguments. It starts with the previously // bound arguments and is extended with the arguments passed now var args = []; for(var i = 1; i < boundArgs.length; i++) args.push(boundArgs[i]); for(var i = 0; i < arguments.length; i++) args.push(arguments[i]); // Now invoke the function with these arguments return f.apply(this, args); } }
46 of 65