What is the every predicate function in Clojure?

What are predicates?

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.

What is the 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.

Syntax

(every? p2 pn)
Syntax of the every function

Where p2 is the predicate function and pn are the parameters to be tested.

Parameter value

The every function receives a predicate function to evaluate the passed parameter, as we will see in an example below.

Return value

The every function returns either true or false, depending on if the passed parameter satisfies the predicate function.

Examples

(ns clojure.examples.example
(:gen-class))
(defn func []
(println (every? zero? '(2 4 6)))
(println (every? even? '(2 4 6))))
(func)

Explanation

  • Line 3: We defined a function called func.
  • Line 4: We use a predicate function, 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.
  • Line 5: We use a predicate function, 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.
  • Line 6: We call the function named func.

Free Resources