Predicates are functions that examine a condition and send a boolean value, which is either true or false. We have predicate functions like the even number. Each predicate function has been discussed in detail in a previous shot. We can search for these shots to learn about the functionality of different predicate functions.
every
function?The every
function takes in a predicate function to evaluate a passed parameter passed. In the case when we want to test if a group number is even, we may use a predicate function called the even
predicate function.
(every? p2 pn)
Where p2
is the predicate function and pn
are the parameters to be tested.
The every
function receives a predicate function to evaluate the passed parameter, as we will see in an example below.
The every
function returns either true
or false
, depending on if the passed parameter satisfies the predicate function.
(ns clojure.examples.example(:gen-class))(defn func [](println (every? zero? '(2 4 6)))(println (every? even? '(2 4 6))))(func)
func
.zero?
, alongside the every
function to evaluate the passed parameter 2 4 6
, and then we print out the output. Notice that the output is false
because even though the values passed are numbers, these values fail the test by the zero
predicate function, as the zero
predicate function checks to see if a number is zero.even?
, alongside the every
function to evaluate the passed parameter 2 4 6
, and then we print out the output. Notice that the output is true
because the values passed are numbers. These values pass the test by the even
predicate function, because the even
predicate function checks to see if a number is even.func
.