Code Reuse - Java

Is there a way to reuse iteration through array code in these functions:

public static double[] ln(double[] z) {
    // returns  an array that consists of the natural logarithm of the values in array z
    int count = 0;

    for (double i : z){
        z[count] = Math.log(i);
        count += 1;
    }
    return z;
}


public static double[] inverse(double[] z) {
    //  returns  an array that consists of the inverse of the values in array z
    int count = 0;

    for (double i : z){
        z[count] = Math.pow(i,-1);
        count += 1;
    }
    return z;
}
+3
source share
7 answers

Yes, using http://en.wikipedia.org/wiki/Strategy_pattern , but this is likely to be redundant. Duplicate iteration and ++ counter don't look so bad.

+7
source

Your code does not match your comments.

public static double[] ln(double[] z) {
    // returns  an array that consists of the natural logarithm of the values in array z
    double[] r = new double[z.length];
    for(int i=0;i<z.length;i++) r[i] = Math.log(z[i]);
    return r;
}

public static double[] inverse(double[] z) {
    //  returns  an array that consists of the inverse of the values in array z
    double[] r = new double[z.length];
    for(int i=0;i<z.length;i++) r[i] = 1/z[i];
    return r;
}

You can use the strategy template to create a common cycle, but this has three sides.

  • The code is much harder to read.
  • The code is longer and harder.
  • Execution is several times slower.

Math.pow (x, -1) is many times more expensive than 1 / x, and can lead to a larger rounding error.

+3

. :

public interface MathOperation {
   public double f(double value);
}

public class Invert implements MathOperation {
   public double f(double value) {
     return Math.pow(value, -1);
   }
}

public class Log implements MathOperation {
   public double f(double value) {    
     return Math.log(value);
   }
}

private static void calculateOnArray(double[] doubles, MathOperation operation) {
  int count = 0;

  for (double i : doubles){
    doubles[count] = operation.f(i);
    count += 1;
  }
}

public static double[] ln(double[] z) {
  calculateOnArray(z, new Log());
  return z;
}

public static double[] inverse(double[] z) {
  calculateOnArray(z, new Invert());
  return z;
}

- , . , downvotes, ; -)

+2

, - -, , , , , . -, for, , count.

, , , , - , , , . , ( , !)

+1

, - , (, Ruby), . Java , , . , , , . :

public interface Calculation {
    double calculate(double x);
}

public static double[] calcArray(double[] z, Calculcation calc) {
    int count = 0;

    for (double i : z){
        z[count] = calc.calculate(i);
        count += 1;
    }
    return z;
}

public static double[] ln(double[] z) {
    return calcArray(z, new Calculation() {
        double calculate(double x) {
            return Math.log(x);
        }
    });
}

public static double[] inverse(double[] z) {
    return calcArray(z, new Calculation() {
        double calculate(double x) {
            return Math.pow(x, -1);
        }
    });
}
+1

, . , , . Guava (http://code.google.com/p/guava-libraries/), , Functional Java (http://functionaljava.org/), .

, , . :

  • x

"x" , . , "", . ( Java , , , .)

, , , :

:


import com.google.common.base.Function;

public class Inverse implements Function
{
    @Override
    public Double apply(Double arg0)
    {
        return Math.pow(arg0, -1);
    }
}

:


import com.google.common.base.Function;

public class Logarithm implements Function
{
    @Override
    public Double apply(Double arg0)
    {
        return Math.log(arg0);
    }
}

:


import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.Test;

import com.google.common.collect.Collections2;

public class Driver
{
    @Test
    public void testInverse()
    {
        List initialValues = Arrays.asList(new Double[] {1.0, 2.0, 3.0});

        List logValues = new ArrayList(Collections2.transform(initialValues, new Inverse()));

        assertEquals(3, logValues.size());

        assertEquals(Double.valueOf(1.0), logValues.get(0), 0.01);
        assertEquals(Double.valueOf(0.5), logValues.get(1), 0.01);
        assertEquals(Double.valueOf(0.333), logValues.get(2), 0.01);

    }
}

Driver , . Collections2.transform testInverse . :

http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/collect/Collections2.html#transform%28java.util.Collection,%20com.google.common.base.Function%29

, , , - , , . , (). , . , .

, , . , (, Guava), . , , , , , , / - ; .

+1

, . , , FOR . , . ?

- - , , , - - , , - .

+1

Source: https://habr.com/ru/post/1786155/


All Articles