Skip to content
Snippets Groups Projects
Commit 495846e6 authored by Christopher Schankula's avatar Christopher Schankula :earth_africa:
Browse files

example of using lambda functions with a class

parent 84f94a6a
No related branches found
No related tags found
No related merge requests found
......@@ -5,4 +5,5 @@
*.aux
*.bbl
*.blg
*DS_STORE*
*DS_Store*
*.csv
package sandbox;
public interface BinaryIntExpression{
public int eval(int a1, int a2);
}
\ No newline at end of file
package sandbox;
public class TestLambdas {
public static void main(String[] args) {
BinaryIntExpression b0;
BinaryIntExpression b1;
BinaryIntExpression b2;
BinaryIntExpression b3;
BinaryIntExpression b4;
int result;
b0 = (a1, a2) -> a1 + a2;
b1 = (a1, a2) -> a1 - a2;
b2 = (a1, a2) -> a1 * a2;
b3 = (a1, a2) -> a1 / a2;
b4 = (a1, a2) -> {
int n = a1 * a2;
n = n * a1 + a2;
return n;
};
result = binaryOperation(1,2,b0);
System.out.println(result);
result = binaryOperation(1,2,b1);
System.out.println(result);
result = binaryOperation(1,2,b2);
System.out.println(result);
result = binaryOperation(1,2,b3);
System.out.println(result);
result = binaryOperation(1,2,b4);
System.out.println(result);
}
public static int binaryOperation(int a1, int a2, BinaryIntExpression exp) {
return exp.eval(a1, a2);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment