This code produces an aggregate Predicate via IntPredicate.and()
method, which would work this way:
p = x -> true; // for first stream element `2` -
// which passes the predicate p
// Predicate p is being reassigned while
// peek() sees the element `2`, to
p = x -> true && x % 2 != 0
// The next stream element `3` passes the updated predicate
// And the predicate is being reassigned again while
// peek() sees the element `3` to
p = x -> true && x % 2 != 0 && x % 3 != 0
// And so on...
So every element which successfully passes the filter
results in a new condition being appended to the current predicate via "logical AND" &&
.
At the end of the stream execution, the predicate would consist of conditions that check the given number against all the prime number from that would be present in the result.
Note that this hacky implementation is broken:
peek()
is an operation which was introduced exclusively to support debugging. It's not meant to be used for performing actions that could affect the result of the execution. peek
gives no guaranty regarding the order in which it will be called while executing in parallel, and in certain cases can be optimized away.