I was elated and horrified to see the scope of functional programming covered in Chapter 4 in Jeanne and Scott's book.
Horrified, because there was so much in there.
Then elated, because I want to know all that stuff anyway, so now I have an excuse to tell my wife, next boss, etc.-- thank you OCJP.
Anyway, both the book and a tutorial video I watched were a bit skimpy on details of how .and() and .or() really worked.
I had two questions immediately upon seeing them:
a. Short Circuit or Not?
b. Do we get the equivalent of operator precedence so that just chaining .and() and .or() is the same thing as writing expressions with && and || ?
A quick glance at the docs told me "Yes" for the first part. Not quick enough, I had already written noisy methods to demonstrate the answer to (a) visually.
I stared at it until I was going cross-eyed, and couldn't tell the answer to (b) from the docs. It seemed like it would potentially be possible to do, but beyond what an API would be expected to do. So my guess was "No" for part B but after reviewing the tutorial video and the chapter material it seemed to still be ambiguous.
I threw together the following quick little demo to show the answer to (b). It does make use of method references as well, primarily because I wanted to be able to have a "noisy" version that illustrated the calls (commented out below). It looks like this:
Literally, just walking away from this I couldn't remember where it made a difference, but knew it did.
This program highlights the difference between the normal short-circuit expression:
T || F && F ==> T
and the composed predicate:
T.or( F ).and( F) ==> F
I don't know if this is quite in scope for the 819 exam or not, but my brain wouldn't let me focus on other things until I understood the answer to by doubt in (b) anyway.
Lastly, I threw in a neat convenience static method new to
Java 11+ at the end, to prove I read the docs. It took me a little while to figure out exactly how to call it.
I think it makes more sense and results in more literate code. The old way of reversing the polarity of a complex predicate combination was clear and readable, NOT!
Oh wait, that is already a nerdy joke trope, no doubt suggesting the static convenience method we got in Java 11.
Comment it out if you are running with Java 8, 9 or 10.