Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

clojure
every method
predicate

What is the every predicate function in Clojure?

Chinweuba Elijah Azubuike

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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)
Example of the every function

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.

RELATED TAGS

clojure
every method
predicate

CONTRIBUTOR

Chinweuba Elijah Azubuike

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring