Created
August 5, 2012 19:23
-
-
Save jnape/3266786 to your computer and use it in GitHub Desktop.
Functional solution for computing geometric mean in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.jnape.dynamiccollection; | |
import com.jnape.dynamiccollection.lambda.Accumulator; | |
import static com.jnape.dynamiccollection.factory.DynamicListFactory.list; | |
import static java.lang.Math.pow; | |
public class GeometricMean { | |
public static final Accumulator<Integer, Integer> TIMES = new Accumulator<Integer, Integer>() { | |
@Override | |
public Integer apply(Integer product, Integer number) { | |
return product * number; | |
} | |
}; | |
public static Number geometricMean(Integer... numbers) { | |
return pow(list(numbers).reduce(TIMES), 1f / numbers.length); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment