Created
January 1, 2020 03:48
-
-
Save joshiraez/f2782211a6f28fdad6f50f554afd7a51 to your computer and use it in GitHub Desktop.
Second example for "Rethinking interfaces"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OperationInterfaceAsClass { | |
Predicate<String> isOperation; | |
Function<List<Integer>, Integer> operate; | |
private OperationInterfaceAsClass(){}; | |
public static OperationInterfaceAsClass of( | |
Predicate<String> isOperation, | |
Function<List<Integer>, Integer> operate | |
){ | |
OperationInterfaceAsClass toBuild = new OperationInterfaceAsClass(); | |
toBuild.isOperation = isOperation; | |
toBuild.operate = operate; | |
return toBuild; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program { | |
public static void main(String[] args) { | |
//Note, because now we can create them on the fly, we win in freedom as to where and how | |
//to define them. | |
var Sum = OperationInterfaceAsClass.of( | |
operator -> operator.equals("+"), | |
numbers -> numbers | |
.stream() | |
.mapToInt(Integer::intValue) | |
.reduce( | |
(a,b) -> a+b | |
).getAsInt() | |
); | |
var Times = OperationInterfaceAsClass.of( | |
operator -> operator.equals("x"), | |
numbers -> numbers | |
.stream() | |
.mapToInt(Integer::intValue) | |
.reduce( | |
(a,b) -> a*b | |
).getAsInt() | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment