Datasets:
ArXiv:
License:
File size: 1,950 Bytes
c574d3a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
package rd222dv_assign1;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
public class FunctionPointers {
public static void main(String[] args) {
System.out.println("Part 1: Apply predicates");
List<Integer> list = Arrays.asList(45, 3, 24, 16, 1, 1, 3, 8, 7, 6, 10, 12, 17, 22, 30);
System.out.print("Print all numbers: ");
Predicate<Integer> all = n -> true;
selectAndPrint(list, all); //
System.out.print("\nPrint all odd numbers: ");
Predicate<Integer> odd = n -> n % 2 != 0; // Must be updated
selectAndPrint(list, odd);
System.out.print("\nPrint all numbers greater than 10: ");
Predicate<Integer> aboveTen = n -> n > 10; // Must be updated
selectAndPrint(list, aboveTen);
System.out.println("\n\nPart 2: Apply functions");
List<Double> numbers = Arrays.asList(1.0, 16.0, 25.0, 81.0);
System.out.println("Original: " + numbers);
System.out.println("Square root: " + applyFunction(numbers, Math::sqrt));
System.out.println("Power of two: " + applyFunction(numbers, FunctionPointers::powerOfTwo));
}
// Prints all elements in the list where predicate evaluates to true
public static void selectAndPrint(List<Integer> list, Predicate<Integer> predicate) {
for(Integer n: list) {
if(predicate.test(n)) {
System.out.print(n + " ");
}
}
}
// Returns a new list containing the numbers resulting from applying fx
// on the input list numbers
private static List<Double> applyFunction(List<Double> numbers, Function<Double, Double> fx) {
List<Double> appliedNumbers = new ArrayList<>();
for (Double n : numbers) {
appliedNumbers.add(Double.valueOf(fx.apply(n)));
}
return appliedNumbers;
}
private static Double powerOfTwo(Double d) {
return d*d; // Must be updated
}
} |