scala predicate
Scala Predicate
In Scala, a predicate is a function that takes an input and returns a Boolean value. It is commonly used to test whether a certain condition is true or false for a given input.
Here is an example of a Scala predicate function that checks if a number is even:
def isEven(number: Int): Boolean = {
number % 2 == 0
}
In this example, the isEven
function takes an Int
parameter called number
and uses the modulo operator %
to check if the number is divisible by 2 without a remainder. If the condition is true, the function returns true
; otherwise, it returns false
.
You can use this predicate function to test if a number is even like this:
val number = 4
val isNumberEven = isEven(number)
println(s"The number $number is even: $isNumberEven")
This will output:
The number 4 is even: true
You can define predicates for various conditions and use them in your Scala code to perform conditional operations or filtering based on those conditions.