/*
 * Copyright 1998-2006 Helma Project
 * Copyright 2010 Hannes Wallnöfer
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * @fileoverview Provides utility functions for working with JavaScript Arrays.
 */

export('contains', 'peek', 'remove', 'union', 'intersection', 'max', 'min', 'partition');

/**
 * check if an array passed as argument contains
 * a specific value (start from end of array)
 * @param {Object} val the value to check
 * @returns {boolean} true if the value is contained
 */
function contains(array, val) {
       return array.indexOf(val) > -1;
}

/**
 * Return the last element of this array. This is like pop(), but
 * without modifying the array.
 * @returns {Object} the last element of the array, or undefined if the array is empty.
 */
function peek(array) {
    return array[array.length - 1];
}

/**
 * Remove the first occurrence of the argument value from this array. This method
 * mutates and returns the array on which it is called and does not create a
 * new array instance.
 * @param {Object} val the value to remove
 * @returns {Array} this array
 */
function remove(array, val) {
    var index = array.indexOf(val);
    if(index > -1) {
        array.splice(index, 1);
    }
    return array;
}

/**
 * Retrieve the union set of a bunch of arrays
 * @param {Array} array1,... the arrays to unify
 * @returns {Array} the union set
 */
function union() {
    var result = [];
    var map = new java.util.HashMap();
    for (var i = 0; i < arguments.length; i += 1) {
        for (var n in arguments[i]) {
            var item = arguments[i][n];
            if (!map.containsKey(item)) {
                result.push(item);
                map.put(item, true);
            }
        }
    }
    return result;
}

/**
 * Retrieve the intersection set of a bunch of arrays
 * @param {Array} array1,... the arrays to intersect
 * @returns {Array} the intersection set
 */
function intersection(array) {
    var all = union.apply(null, arguments);
    var result = [];
    for (var n in all) {
        var chksum = 0;
        var item = all[n];
        for (var i = 0; i < arguments.length; i += 1) {
            if (contains(arguments[i], item))
                chksum += 1;
            else
                break;
        }
        if (chksum == arguments.length)
            result.push(item);
    }
    return result;
}

/**
 * @returns the maximal element in an array obtained by calling Math.max().
 */
function max(array) {
    return Math.max.apply(Math, array);
}

/**
 * @returns the minimal element in an array obtained by calling Math.min().
 */
function min(array) {
    return Math.min.apply(Math, array);
}

function partition(fn) {
        var trues = [], falses = [];
        for (var i=0; i<this.length; i++) {
            if (fn(this[i], i)) {
                trues.push(this[i]);
            } else {
                falses.push(this[i]);
            }
        }
        return [trues, falses]
    }


@@ -3198,28 +3198,24 @@
 ition(fn) %7B%0A
-    
     var true
@@ -3235,20 +3235,16 @@
 s = %5B%5D;%0A
-    
     for 
@@ -3279,28 +3279,24 @@
 ) %7B%0A        
-   
- 
 if (fn(this%5B
@@ -3301,28 +3301,24 @@
 s%5Bi%5D, i)) %7B%0A
-    
             
@@ -3346,20 +3346,16 @@
         
-    
 %7D else %7B
@@ -3367,20 +3367,16 @@
         
- 
-   
 falses.p
@@ -3397,36 +3397,24 @@
         
-    %7D%0A   
+%7D%0A
     
- 
 %7D%0A
-    
     retu
@@ -3436,11 +3436,6 @@
 es%5D%0A
-    
 %7D%0A
-%0A

Fix indentation