Lambda expression / declaration parameters

I've been logged in for a while and don't usually post unless I really can't seem to find an answer, and now I feel pretty stuck.

I just recently started getting familiar with java Lambda expressions (mostly functions) like BinaryOperator and Bifunction ..) I've looked at sources for these and I see that you are declaring them like, for example:

public interface BiFunction<T, U, R> {...}.

      

So, it seems to me that there is no way to dynamically create an interface (similar to BiFunction), but with a number of parameters and "equations" set at runtime.

I'll give you a practical example that I think will greatly improve the type of problem I would like to solve:

Suppose I wanted to create a program where the user could enter a math function and evaluate it at a point. First, let's assume it's one variable, so:

Function<Integer,Integer> f1 = (x) -> Math.pow(x,2);

      

would be the solution to the problem, except that before defining the time, I define the function f (x) = x ^ 2. I would like to know if there is a way to create a function at runtime by asking the user which function they would like to enter.

This problem can be more difficult if the user can enter equations with multiple parameters.

Thanks for any help.

Max

+3


source to share


1 answer


What you need for this is curry. For a function f

that takes parameters a

, b

and c

, you can define a curry version f

that takes a parameter a

and spits out a function g

that takes parameters b

and c

. Continue the currying process and you only have three worries:

  • Null arguments. Call the function.
  • One argument. Call the function with the given parameter.
  • Two or more arguments. Curry function and reevaluate because curried function will have reduced arity.


So, if you want to use a lambda, you need to define a lambda, which is a function. Of course, you also probably need to define the appropriate operators for whatever algebra you would like to support at runtime, but this is probably a fixed symbol table (and can probably be defined in the same way as hastable functions / lambdas accepting arguments).

+2


source







All Articles