nashorn/samples/word_histogram.js
author mgerdin
Mon, 11 Aug 2014 17:12:41 +0400
changeset 25958 8dc85547d6d6
parent 24639 cde22f00c4e6
child 26067 b32ccc3a76c9
permissions -rw-r--r--
8011397: JTREG needs to copy additional WhiteBox class file to JTwork/scratch/sun/hotspot Reviewed-by: kvn, iignatyev, mgerdin Contributed-by: andrey.x.zakharov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24639
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
     1
#nashorn word histogram of a file
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
     2
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
     3
/*
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
     4
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
     5
 * 
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
     6
 * Redistribution and use in source and binary forms, with or without
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
     7
 * modification, are permitted provided that the following conditions
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
     8
 * are met:
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
     9
 * 
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    10
 *   - Redistributions of source code must retain the above copyright
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    11
 *     notice, this list of conditions and the following disclaimer.
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    12
 * 
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    13
 *   - Redistributions in binary form must reproduce the above copyright
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    14
 *     notice, this list of conditions and the following disclaimer in the
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    15
 *     documentation and/or other materials provided with the distribution.
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    16
 * 
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    17
 *   - Neither the name of Oracle nor the names of its
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    18
 *     contributors may be used to endorse or promote products derived
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    19
 *     from this software without specific prior written permission.
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    20
 * 
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    21
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    22
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    23
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    24
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    25
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    26
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    27
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    28
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    29
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    30
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    31
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    32
 */
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    33
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    34
/*
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    35
 * This example demonstrates how to print word histogram
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    36
 * of a given text file using regex, array and JSON
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    37
 * functions.
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    38
 */
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    39
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    40
if (arguments.length < 1) {
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    41
    print("Usage: jjs -scripting word_histogram.js -- <file>");
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    42
    exit(1);
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    43
}
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    44
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    45
var obj = {};
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    46
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    47
readFully(arguments[0]).
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    48
    split(/[^\w+]/).
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    49
    forEach(function(x)
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    50
      (x in obj? obj[x]++ : obj[x] = 1));
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    51
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    52
print(JSON.stringify(obj));
cde22f00c4e6 8044415: ant makefile should have a target to generate javadoc only for jdk.nashorn.api and sub-packages
sundar
parents:
diff changeset
    53