can you use a boolean predicate on its own as the parameter for an if statement
Strictly speaking, the answer to this is 'no', because a predicate is a function (in the mathematical sense). What you can use for the conditional expression in an if
is any boolean expression, including (as here) the result of the invocation of a predicate on a value.
To expand, the 'predicate' here is 'equals HELLO'. It is a function (in the mathematical sense) that operates on values of type string
, and returns boolean values. Once you have obtained a boolean value (by applying this function to a particular string, str
), you do not need to explicitly compare it to true
: you can just use it.
As you will see from others' answers, code in which expressions of boolean type are explicitly compared to boolean literals will often cause code-style pain in the reader :) (My 'favourite' peeve is <boolean expression> ? true : false
).