Skip to content

Instantly share code, notes, and snippets.

@joshiraez
Created January 1, 2020 03:48
Show Gist options
  • Save joshiraez/f2782211a6f28fdad6f50f554afd7a51 to your computer and use it in GitHub Desktop.
Save joshiraez/f2782211a6f28fdad6f50f554afd7a51 to your computer and use it in GitHub Desktop.
Second example for "Rethinking interfaces"
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;
}
}
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