Re: Dealing with higher order operations coupled with primitives

Discussion in 'Java' started by Stefan Ram, Jun 22, 2012.

  1. Stefan Ram

    Stefan Ram Guest

    "Aaron W. Hsu" <> writes:
    >Where I can reuse the already allocated, potentially very large internal
    >primitive data structure in res multiple times, including across function
    >applications, instead of requiring a copy of that large structure (these
    >structures could, for instance, be gigabyte arrays).


    public class Main
    { public static void main( final java.lang.String[] args )
    { { final int[] array = new int[]{ 2, 3, 4 };
    final Operation sum = new Sum();
    java.lang.System.out.println( Util.map( sum, array )); }
    { final double[] array = new double[]{ 2, 3, 4 };
    final Operation product = new Product();
    java.lang.System.out.println( Util.map( product, array )); }}}

    /* prints:
    9
    24.0
    */

    interface Operation
    { java.lang.Object of( final int[] array );
    java.lang.Object of( final double[] array ); }

    class Sum implements Operation
    { public java.lang.Object of( final int[] array )
    { int result = 0; for( final int comp: array )result += comp;
    return result; }
    public java.lang.Object of( final double[] array )
    { double result = 0; for( final double comp: array )result += comp;
    return result; }}

    class Product implements Operation
    { public java.lang.Object of( final int[] array )
    { int result = 1; for( final int comp: array )result *= comp;
    return result; }
    public java.lang.Object of( final double[] array )
    { double result = 1; for( final double comp: array )result *= comp;
    return result; }}

    class Util
    { public static java.lang.Object map
    ( final Operation operation, final int[] array )
    { return operation.of( array ); }
    public static java.lang.Object map
    ( final Operation operation, final double[] array )
    { return operation.of( array ); }}
     
    Stefan Ram, Jun 22, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gina Engli
    Replies:
    0
    Views:
    295
    Gina Engli
    Jun 22, 2012
  2. Stefan Ram
    Replies:
    0
    Views:
    278
    Stefan Ram
    Jun 22, 2012
  3. Gina Engli
    Replies:
    10
    Views:
    471
    Borg Queen
    Jun 23, 2012
  4. Roedy Green
    Replies:
    0
    Views:
    306
    Roedy Green
    Jun 22, 2012
  5. Stefan Ram
    Replies:
    0
    Views:
    287
    Stefan Ram
    Jun 22, 2012
Loading...

Share This Page