From dde16d49785d022ec98163638b62eaababcdffa7 Mon Sep 17 00:00:00 2001 From: Schankula Christopher <schankuc@mcmaster.ca> Date: Sat, 24 Feb 2018 17:01:45 -0500 Subject: [PATCH] example of parameterized lambda functions --- src/sandbox/GeneralCompare.java | 5 +++++ src/sandbox/TestLambdas.java | 17 +++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 src/sandbox/GeneralCompare.java diff --git a/src/sandbox/GeneralCompare.java b/src/sandbox/GeneralCompare.java new file mode 100644 index 0000000..29332aa --- /dev/null +++ b/src/sandbox/GeneralCompare.java @@ -0,0 +1,5 @@ +package sandbox; + +public interface GeneralCompare<T> { + public int eval(T a1, T a2); +} diff --git a/src/sandbox/TestLambdas.java b/src/sandbox/TestLambdas.java index 69ac2bc..91dc35c 100644 --- a/src/sandbox/TestLambdas.java +++ b/src/sandbox/TestLambdas.java @@ -7,9 +7,9 @@ package sandbox; public class TestLambdas { public static void main(String[] args) { //declare the instances of BinaryIntExpression - BinaryIntExpression b0; - BinaryIntExpression b1; - BinaryIntExpression b2; + GeneralCompare<Integer> b0; + GeneralCompare<Integer> b1; + GeneralCompare<Integer> b2; BinaryIntExpression b3; BinaryIntExpression b4; BinaryIntExpression b5; @@ -34,13 +34,13 @@ public class TestLambdas { }; //call the main function using the instances as a parameter. - result = binaryOperation(1,2,b0); + result = binaryIntegerOperation(1,2,b0); System.out.println(result); - result = binaryOperation(1,2,b1); + result = binaryIntegerOperation(1,2,b1); System.out.println(result); - result = binaryOperation(1,2,b2); + result = binaryIntegerOperation(1,2,b2); System.out.println(result); result = binaryOperation(1,2,b3); @@ -61,4 +61,9 @@ public class TestLambdas { return exp.eval(a1, a2); } + public static int binaryIntegerOperation(int a1, int a2, GeneralCompare<Integer> exp) { + //call the eval() function in the given BinaryIntExpression. + return exp.eval(a1, a2); + } + } -- GitLab