2
* Copyright 2009 Google Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS-IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
18
* @fileOverview A collection of core JavaScript utilities.
22
* Iterator convenience for JavaScript Objects.
24
* Note that if func returns false, the iteration will be immediately terminated.
25
* (Returning undefined, or not specifying a return type, does not terminate the iteration).
33
eachProperty(pastels, function(key, value) {
34
print(DIV({style: 'background: '+value+';'}, key));
37
* @param {object} obj The object over which to iterate.
38
* @param {function} func The function to run on each [key,value] pair.
40
function eachProperty(obj, func) {
43
if (!obj.hasOwnProperty || obj.hasOwnProperty(k)) {
53
* Douglas Crockford's "object" function for prototypal inheritance, taken from
54
* http://javascript.crockford.com/prototypal.html
56
* @param {object} parent The parent object.
57
* @return {object} A new object whose prototype is parent.
59
function object(parent) {
66
* Creates an array of the properties of <code>obj</code>,
67
* <em>not</em> including built-in or inherited properties. If no
68
* argument is given, applies to the global object.
72
keys({a: 1, b: 2, c: 3}).forEach(function(k) {
77
// Prints all the functions and object members of the global "appjet" object,
79
print(keys(appjet).join('\n'));
90
if (!obj.hasOwnProperty || o.hasOwnProperty(k)) {
98
* Comparator that returns -1, +1, or 0 depending on whether a < b, or a > b, or
99
* neither, respectively.
102
* @return {number} -1, 0, or +1
114
function arrayToSet(arr) {
116
arr.forEach(function(x) {
122
function mergeArrays(mergeFunction, a1, a2, etc) {
124
var arrays = Array.prototype.slice.call(arguments, 1);
125
for (var i = 0; i < arrays.length; ++i) {
126
if (arrays[i].length != len) {
131
for (var i = 0; i < a1.length; ++i) {
132
out.push(mergeFunction.apply(this, arrays.map(function(array) { return array[i]; })));
137
function debug(obj) {
138
if (typeof(obj) == 'object') {
141
eachProperty(obj, function(k, v) {
142
ret.push(k+" -> "+debug(v));
144
return '['+ret.join(", ")+']';
154
* Create a scala function out of the given JS function.
156
function scalaFn(nargs, f) {
157
if (typeof(f) == 'function') {
158
return new Packages.scala['Function'+nargs]({
162
return new Packages.scala['Function'+nargs]({
163
apply: function() { return f; }
168
function scalaF0(f) {
169
return scalaFn(0, f);
172
function scalaF1(f) {
173
return scalaFn(1, f);
177
* Some bonus functions for functional programming.
179
function f_curry(thisPtr, f, arg1, arg2, etc) {
180
var curriedArgs = Array.prototype.slice.call(arguments, 2);
182
var args = Array.prototype.slice.call(arguments, 0);
183
return f.apply(thisPtr, curriedArgs.concat(args));
187
function f_limitArgs(thisPtr, f, n) {
189
var args = Array.prototype.slice.call(arguments, 0, n);
190
return f.apply(thisPtr, args);