Diagnostic Quiz
Problems
*1-7. Application of Monad Laws
Trace through the program step by step to see if the certain class/type logic follows the three Monad Laws anot.
Question 1-6 are actually proving that this type A follows the three monad laws. Among these questions, I think those related to the associative law are worth trying again!
class A {
private final int value;
private final int cumulative;
public A(int value, int cumulative) {
this.value = value;
this.cumulative = cumulative;
}
public static A of(int value) {
return new A(value, 0);
}
public A flatMap(Transformer<Integer, A> transformer) {
A updated = transformer.transform(this.value);
return new A(updated.value, updated.cumulative + this.cumulative);
}
public String toString() {
return this.value + " " + this.cumulative;
}
}*5. What does the following code evaluate to?
By tracing through the program step by step, we can start from left to right, and then combine the result from right to left. The answer should be 4 3.
8. Parallel and Concurrent Programming
Having multiple cores/processors is a prerequisite to running a program in parallel.
9. Requirements for a stream to be parallelized
The stream operations pipeline should not have:
an operation with a side effect
an operation that interferes with the data source.
an operation that is stateful
*14. More on reduce()
reduce()Include the three rules for
reduce()to be parallelizable into cheatsheet!

This questions explicitly asks for the compatibility rule, so we want to test
Starting from L.H.S, accumulator.apply(identity, t) = 2 * t, then combiner.apply(u, 2 * t) = u * 2 * t * 2 = u * t * 4.
R.H.S accumulator.apply(u, t) = u * t * 2.
Since L.H.S R.H.S, it fails the compatibility rule.
Starting from L.H.S, accumulator.apply(identity, t) = 2 * t, then combiner.apply(u, 2 * t) = u * 2 * t = u * t * 2.
R.H.S accumulator.apply(u, t) = u * t * 2.
Since L.H.S R.H.S, it passes the compatibility rule.
Starting from L.H.S, accumulator.apply(identity, t) = 2 * t, then combiner.apply(u, 2 * t) = u * 2 * t = u + t * 2.
R.H.S accumulator.apply(u, t) = u * t * 2.
Since L.H.S R.H.S, it fails the compatibility rule.
Starting from L.H.S, accumulator.apply(identity, t) = 2 * t, then combiner.apply(u, 2 * t) = u * 2 * t = u * t * 2 / 2 = u * t.
R.H.S accumulator.apply(u, t) = u * t * 2.
Since L.H.S R.H.S, it fails the compatibility rule.
15. Requirements for reduce() to be safe to be parallelized
reduce() to be safe to be parallelizedFor a
reduce(identity, accumulator, combiner)operation to be safe and correct for parallel execution, it must follow these three rules:
Identity Rule
Associativy Rule
Compatibility Rule
Tips
Trace through the program step by step to see if the certain class/type logic follows the three Monad Laws anot.
Having multiple cores/processors is a prerequisite to running a program in parallel.
The stream operations pipeline should not have:
an operation with a side effect
an operation that interferes with the data source.
an operation that is stateful
Include the three rules for
reduce()to be parallelizable into cheatsheet!For a
reduce(identity, accumulator, combiner)operation to be safe and correct for parallel execution, it must follow these three rules:Identity Rule
Associativy Rule
Compatibility Rule
Last updated