// This work is Copyright(c) 2008 John Miller. All rights reserved. /* * @test * @summary Test use of closures for computing statistics * Closures are to be included in the next version of JSIM. * This code was tested using Neal Gafter's prototype that * supports the BGGA closure proposal. * http://www.javac.info/closures.tar.gz (updated 2008-04-14) * @author John Miller */ import static java.lang.System.*; import static java.lang.Math.*; public class StatsClosure { static void stats (double [] v, { double => double } f) { for (double x : v) f.invoke (x); } // stats public static void main (String [] args) { @Shared double y; double [] data = { 20.0, 10.0, 30.0, 50.0, 40.0 }; double size = (double) data.length; y = 0.0; stats (data, { double x => y += x }); out.println ("mean\t = " + y / size ); y = 0.0; stats (data, { double x => y += x * x }); out.println ("stddev\t = " + sqrt (y / (size - 1.0))); y = Double.MAX_VALUE; stats (data, { double x => y = x < y ? x : y }); out.println ("min\t = " + y); y = Double.MIN_VALUE; stats (data, { double x => y = x > y ? x : y }); out.println ("max\t = " + y); } // main } // StatsClosure class