0

I have a kind of calculator:

sum = value1 + value2;
    multiplication = value1 * value2;
    division = (value1 / value2);
    average = (sum / 2);

    if (operation.compareToIgnoreCase("add") == 0)
        result = sum;
    else if (operation.compareToIgnoreCase("mult") == 0)
        result = multiplication;
    else if (operation.compareToIgnoreCase("div") == 0)
        result = division;
    else if (operation.compareToIgnoreCase("avg") == 0)
        result = average;

And i have to turn this to lambda expressions:

result = (request.getValue1(), request.getValue2()) -> {
        if (request.getOperation().compareToIgnoreCase("add") == 0) return request.getValue1() + request.getValue2();
        else if (request.getOperation().compareToIgnoreCase("mult") == 0) return request.getValue1() * request.getValue2();
        else if (request.getOperation().compareToIgnoreCase("div") == 0) return request.getValue1() / request.getValue2();
        else if (request.getOperation().compareToIgnoreCase("avg") == 0) return (request.getValue1() + request.getValue2())/2;
    }

But it doesnt work. Can you guys help? Thanks

3
  • This is not the lambda issue but the design issue. Commented Sep 14, 2018 at 12:57
  • In addition to everything else, the point of using polymorphism is to use a Map<String,something> instead of those if statements. Commented Sep 14, 2018 at 13:03
  • What you mean with design issue @Nikolas? Commented Sep 17, 2018 at 8:34

3 Answers 3

8

For example:

BiFunction<Number,Number,Number> sum = (value1, value2) -> value1 + value2;

and you can use it like this:

sum.apply(value1,value2)

more examples here.

Also you can store your set of BiFunction in a Map:

Map<String,BiFunction<Number, Number, Number>> operations = new HashMap<>();
operations.put("sum", sum);

If you do the same for all operations, you call them as it follows:

operations.get("sum").apply(value1, value2);

So, the result can be computed as is follows:

result = (value1, value2, operation) -> operations.get(operation).apply(value1, value2);
Sign up to request clarification or add additional context in comments.

4 Comments

much cleaner would be to define an enum for this
Yes, you can improve the solution using enums. This is just a basic example, but enumeration fits the use case pretty well and the get operation should handle unknown operations.
Why the lambda at the end? what is this 3-arg lambda?
To replace the last lambda reported in question.
3

You can create a map with a String and DoubleBinaryOperator or an IntBinaryOperator depends on the type you want like so :

Map<String, DoubleBinaryOperator> map = new HashMap<>();
map.put("add", (v1, v2) -> v1 + v2);
map.put("mult", (v1, v2) -> v1 * v2);
map.put("div", (v1, v2) -> v1 / v2);
map.put("avg", (v1, v2) -> (v1 * v2) / 2);

Then you can call that map with the operation you want for example :

// an example
String operation = "add";
Double reslt = map.get(operation).applyAsDouble(3, 5);

Comments

0

from the example it's not very clear, what result should be. So let me rewrite it including types:

Function<Request, Double> calculator = (request) -> {
    if (request.getOperation().compareToIgnoreCase("add") == 0) return request.getValue1() + request.getValue2();
    else if (request.getOperation().compareToIgnoreCase("mult") == 0) return request.getValue1() * request.getValue2();
    else if (request.getOperation().compareToIgnoreCase("div") == 0) return request.getValue1() / request.getValue2();
    else if (request.getOperation().compareToIgnoreCase("avg") == 0) return (request.getValue1() + request.getValue2())/2;
    else throw new IllegalArgumentException();
};

double result = calculator.apply(new Request("add", 2, 4));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.