Blame openjdk/nashorn/samples/jsobject_mapreduce.js

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