0

I have a function named myFunction which takes as inputs a list and a function and it applies the function on every element of the list. Example:

list = {1,2,3}
def square(int x):
     return x*x;
myFunction(list , square) should return {1 ,4, 9}

Now here is the catch, the user can give any function as the input. I know that the functions can be wrapped in the interface and passed as arguments. But in my case, i wouldn't know the name of the function to begin with. Is there any way to deal with this?

Aditya Naidu
  • 697
  • 2
  • 7
  • 18
  • @Satya I have passed methods as function parameter by making an interface. This interface is then implemented by the class which needs this function as a parameter to another function. – Aditya Naidu Feb 04 '16 at 14:01
  • But now the situation is different, The functions are user defined. The functions can change each time depending on what the user want. He may want to square it, or take a square root etc,. – Aditya Naidu Feb 04 '16 at 14:02
  • 1
    I vote for removing the java tag, as the example code rather looks like python. – xXliolauXx Feb 04 '16 at 14:06
  • @xXliolauXx Relax, it's just an example of what I wanted to do. You can't simply discard the java tag as interface is implemented in java which is used to pass the function. – Aditya Naidu Feb 04 '16 at 14:08
  • If you want a java answer, post actual java code – OneCricketeer Feb 04 '16 at 14:10
  • i think the question about using delegates in java? please, see http://stackoverflow.com/questions/1340231/equivalent-of-c-sharp-anonymous-methods-in-java – prsmax Feb 04 '16 at 14:18

2 Answers2

0

If you use Java 8, you could take a function (this assumes that the function returns the same type as its input parameter):

public <T> myFunction(T parameter, java.util.function.Function<T, T> funct){
   return funct.apply(parameter);
}

Alternatively, you could use a functional interface and call it using a lambda expression, or even an anonymous class as suggested by others:

interface IntDealer{
    int apply(int param);
}
//Thus have your function defined like this:
public int myFunction(int param, IntDealer impl) {
    return impl.apply(int);
}

In this way, you could call it with a lambda or an anonymous class.

ernest_k
  • 44,416
  • 5
  • 53
  • 99
0

You could use Lambda expressions in Java 8.

Lets' say you have class A that includes your base method

class A{
     int square(int i){
         return i*i
     }
     // other methods (like square root) could here.
}

You could create class B that has a method that accepts another method and a list like this

class B {
    public List<Integer> myFunction(List<Integer> list, IntConsumer aMethod) {
        List<Integer> sqList=new ArrayList<>()
        for (int i: list){
            sqList.add(aMethod.accept(i))
        }
        return sqList;
    }
}

Finally, you could just call it like this:

new B().myFunction(list, A::suqare)

or

new B().myFunction(list, A::suqareRoot)

P.S: The code is just pseudo, you might need to check if it compiles.

Mohamed Taher Alrefaie
  • 15,698
  • 9
  • 48
  • 66