Skip to content

Instantly share code, notes, and snippets.

@kaspernielsen
Created December 18, 2019 10:52
Show Gist options
  • Save kaspernielsen/463921e0326c87205dc1c37e2559fae6 to your computer and use it in GitHub Desktop.
Save kaspernielsen/463921e0326c87205dc1c37e2559fae6 to your computer and use it in GitHub Desktop.
Query Example
public class FilterMapToDoubleSortedSum extends Processor {
public Object process(int[] arr, TerminalQueryOperationNode node) {
// Original Query: C_FILTER->C_MAP_TO_DOUBLE->C_SORTED_NATURAL->CT_MATH_SUM
// Simplified : C_FILTER->C_MAP_TO_DOUBLE->CT_MATH_SUM
// Extract all functions from a linked list of query objects
SI_MapToDouble on = (SI_MapToDouble) node.previous().previous();
IntToDoubleFunction mapper = on.getMapper();
IntPredicate intPredicate = ((SI_Filter) on.previous()).getIntPredicate();
double sum = 0.0d;
for (int i = 0; i < arr.length; i++) {
int e = arr[i];
if (intPredicate.test(e)) {
double mapped = mapper.applyAsDouble(e);
sum += mapped;
}
}
return Double.valueOf(sum);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment