During the last months I have tried to code using the functional programming paradigm. Now I have a solution in OOP and I am trying to find a functional solution.
The problem is simple. I have an algorithm, which produces two different arrays as result (a and b). Now, I want to check how good the results are. Therefore I write several evaluation criteria for them. I hope pseudo-java source code is okay for you!
// first the Algorithm class
class Algorithm {
private []a;
private []b;
Algorithm(input) {
computeResult();
}
getA(){return a;}
getB(){return b;}
void computeResult() {
...
... // time-consuming operations
... // set values for a and b
...
}
}
// this class tests the Algorithm using a list of evaluation criteria
class AlgorithmTest {
AlgorithmTest() {
...
... // Definition of input
... // Definition of list of evaluation criteria evals
...
Algorithm algorithm = new Algorithm(input); // Compute the result
for (EvaluationCriterion eval : evals) {
System.out.println(eval.getClassSimpleName()); // Print name of class
System.out.println(eval.evaluate(algorithm)); // Print evaluation result
}
}
main () {
new AlgorithmTest();
}
}
interface EvaluationCriterion {
double evaluate(Algorithm a);
}
// an example implementation of a criterion
class EvaluationA implements EvalutationCriterion{
double evaluation(Algorithm algorithm) {
a = algorithm.getA();
b = algorithm.getB();
double c = anotherComputation(a, b);
return c;
}
double anotherComputation(a, result){
... // compute and return result
}
}
Is it possible to "transform" this source code using functional programming paradigm? I am sure it it, but can you still add new evaluation criteria easily like in the OOP approach?
I could write a module called algorithm that includes pure functions that compute either a or b. In this case I have to compute it twice, which requires to much time.
But how to do the evaluation step using multiple evaluation functions?