Repository navigation
Expand file tree
/
Copy pathQ5_Lambdas.java
More file actions
71 lines (57 loc) · 2.61 KB
/
Copy pathQ5_Lambdas.java
File metadata and controls
71 lines (57 loc) · 2.61 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
import java.util.function.*;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
public class Q5_Lambdas {
// ── PREDICT: What does main() print for lines 1-3? ──
// Also answer the inline questions.
public static void main(String[] args) {
int multiplier = 3;
IntUnaryOperator triple = x -> x * multiplier;
System.out.println("1: " + triple.applyAsInt(7));
// Q: if you uncomment the next line, does it compile? why?
// multiplier = 4;
Runnable[] tasks = new Runnable[3];
for (int i = 0; i < 3; i++) {
final int idx = i;
tasks[i] = () -> System.out.print(idx);
}
System.out.print("2: ");
for (Runnable r : tasks) r.run();
System.out.println();
// Q: what if you removed "final" from idx? does it still compile?
List<String> motors = Arrays.asList("Falcon", "Neo", "Kraken", "Vortex");
motors.sort((a, b) -> a.length() - b.length());
System.out.println("3: " + motors);
// ── WRITE ──
// 1) Write a method "applyToAll" that takes a List<Integer> and a
// UnaryOperator<Integer>, applies the operator to every element
// IN PLACE (mutates the list), and returns void.
List<Integer> nums = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
applyToAll(nums, x -> x * x);
assert nums.equals(Arrays.asList(1, 4, 9, 16, 25)) : "applyToAll failed: " + nums;
System.out.println("applyToAll ok");
// 2) Write a method "compose" that takes two IntUnaryOperators f and g
// and returns a new IntUnaryOperator that computes f(g(x)).
// Do NOT use .compose() or .andThen() — write the lambda yourself.
IntUnaryOperator doubleIt = x -> x * 2;
IntUnaryOperator addThree = x -> x + 3;
IntUnaryOperator doubleThenAdd = compose(addThree, doubleIt);
assert doubleThenAdd.applyAsInt(5) == 13 : "compose failed"; // addThree(doubleIt(5)) = 13
System.out.println("compose ok");
// 3) Using streams, write one expression that filters nums to only
// values > 5, then sums them. Store result in "total".
int total = 0; // replace this
assert total == 9 + 16 + 25 : "stream sum failed: " + total;
System.out.println("stream ok");
}
// TODO
static void applyToAll(List<Integer> list, UnaryOperator<Integer> op) {
// your code here
}
// TODO
static IntUnaryOperator compose(IntUnaryOperator f, IntUnaryOperator g) {
// your code here
return x -> x;
}
}