I have a matrix class and I want to create a function called map in this class, that looks something like this
public void map(T fn) {
//do something
}
the idea behind the function is that as a parameter fn, which could be any method and apply this method to each number in the matrix.
the Idea behind it was for the coding train videos on a neural network but I tried to manipulate it in Java and it didn't work.I search lambda expressions but when I use it it's only let me pass method like Math.cos(arg) with an argument inside. but I want it to pass without any arguments the current function looks like this
interface lambda<T> {
double foo(T fn);
}
public void map(T fn) {
for(int i = 0 ; i< this.getRow() ; i++) {
for(int j = 0 ; j< this.getCol(); j++) {
int a = i;
int b = j;
lambda mycode = (T) -> setData(matrix[a][b], fn);
matrix[i][j] = mycode.foo(fn);
}
}
}
private double setData(double data, T fn) {
data = (double) fn;
return data;
}
when I send a call from my main class I can only call with arguments like
matrix.map(Math.cos(matrix.getData(0,0))
but it apply on each value in the matrix the cos of data at 0,0
I wish it will look more like this
matrix.map(Math.cos())
or even
Matrix.map(function())
is there a way in java to pass methods as parameters without entering arguments in them?