Blame openjdk/nashorn/samples/jsobject_mapreduce.js

Packit 1b8c31
#// Usage: jjs -scripting -cp . jsobject_mapreduce.js
Packit 1b8c31
Packit 1b8c31
/*
Packit 1b8c31
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
Packit 1b8c31
 *
Packit 1b8c31
 * Redistribution and use in source and binary forms, with or without
Packit 1b8c31
 * modification, are permitted provided that the following conditions
Packit 1b8c31
 * are met:
Packit 1b8c31
 *
Packit 1b8c31
 *   - Redistributions of source code must retain the above copyright
Packit 1b8c31
 *     notice, this list of conditions and the following disclaimer.
Packit 1b8c31
 *
Packit 1b8c31
 *   - Redistributions in binary form must reproduce the above copyright
Packit 1b8c31
 *     notice, this list of conditions and the following disclaimer in the
Packit 1b8c31
 *     documentation and/or other materials provided with the distribution.
Packit 1b8c31
 *
Packit 1b8c31
 *   - Neither the name of Oracle nor the names of its
Packit 1b8c31
 *     contributors may be used to endorse or promote products derived
Packit 1b8c31
 *     from this software without specific prior written permission.
Packit 1b8c31
 *
Packit 1b8c31
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
Packit 1b8c31
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
Packit 1b8c31
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
Packit 1b8c31
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
Packit 1b8c31
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Packit 1b8c31
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Packit 1b8c31
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
Packit 1b8c31
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
Packit 1b8c31
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
Packit 1b8c31
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Packit 1b8c31
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 1b8c31
 */
Packit 1b8c31
Packit 1b8c31
// Many Array.prototype functions such as map,
Packit 1b8c31
// filter, reduce, reduceRight, every, some are generic.
Packit 1b8c31
// These functions accept ECMAScript array as well as
Packit 1b8c31
// many array-like objects including JSObjects.
Packit 1b8c31
// See also http://en.wikipedia.org/wiki/MapReduce
Packit 1b8c31
Packit 1b8c31
`javac BufferArray.java`;
Packit 1b8c31
Packit 1b8c31
var BufferArray = Java.type("BufferArray");
Packit 1b8c31
var buf = new BufferArray(10);
Packit 1b8c31
Packit 1b8c31
var map = Array.prototype.map;
Packit 1b8c31
var filter = Array.prototype.filter;
Packit 1b8c31
var reduce = Array.prototype.reduce;
Packit 1b8c31
Packit 1b8c31
// make random list of numbers
Packit 1b8c31
for (var i = 0; i < 10; i++)
Packit 1b8c31
    buf[i] = Math.random();
Packit 1b8c31
Packit 1b8c31
var forEach = Array.prototype.forEach;
Packit 1b8c31
// print numbers in the list
Packit 1b8c31
forEach.call(buf, function(x) print(x));
Packit 1b8c31
Packit 1b8c31
// print sum of squares of the random numbers
Packit 1b8c31
print("Square sum:",
Packit 1b8c31
    reduce.call(
Packit 1b8c31
        map.call(buf, function(x) x*x),
Packit 1b8c31
        function(x, y) x + y)
Packit 1b8c31
);