Function, Predicate, Supplier
Completed
Using java.util.function: a Predicate<Integer> isPositive, a Function<Integer, Integer> triple and a Function made with triple.andThen(x -> x + 1). For each input number print positive?, the tripled value and the composed value.
Input
n, then n integers.
Output
n lines: bool triple composed
Example
Input
3 5 -2 0
Output
true 15 16 false -6 -5 false 0 1
Editor
Java
Hints
Predicate.test, Function.apply.
andThen runs one function after another.
Solution explanation
The standard functional interfaces let you pass behaviour around as values and compose it.
Show solution
import java.util.Scanner;
import java.util.function.Function;
import java.util.function.Predicate;
public class Main {
public static void main(String[] args) {
Predicate<Integer> isPositive = x -> x > 0;
Function<Integer, Integer> triple = x -> x * 3;
Function<Integer, Integer> tripleThenInc = triple.andThen(x -> x + 1);
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
System.out.println(isPositive.test(x) + " " + triple.apply(x) + " " + tripleThenInc.apply(x));
}
}
}