Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

predicate function
clojure
every- pred
communitycreator

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

Overview

Predicates examine a condition and send a boolean value (true or false). We have several predicate functions, such as even number and zero. You can find shots on predicates and study functionality, as each predicate function has been treated in detail separately.

Let's study the every-pred function. This function takes in multiple predicate functions to evaluate parameters passed. For instance, we use two predicate functions to test if a number is even and not a string: the even predicate function and the number predicate function.

Syntax

(every-pred p1 p2 .. pn)
every-pred syntax

Here:

  • p1 and p2 are the predicate functions.
  • pn is the parameters to be tested.

Parameter

The every-pred function receives multiple predicate functions to evaluate the passed parameter.

Return value

The every-pred returns either true or false depending on if the passed parameter satisfies all of the predicates function.

Example

(ns clojure.examples.example
(:gen-class))
(defn func []
(println ((every-pred number? zero?) 2 4 6))
(println ((every-pred number? even?) 2 4 6)))
(func)
Example on the every-pred function

Explanation

  • Line 3: We define a function, func.
  • Line 4: We use two predicate functions (number, zero) alongside the every-pred function to evaluate the passed parameter 2 4 6 . Next, we print the output to console. Notice that the output is false. We use the number predicate function to check if the passed values, which are numbers, fail the test by the zero predicate function. We use the zero predicate function to see if a number is zero.
  • Line 5: We use two predicate functions (number, even) alongside the every-pred function to evaluate the passed parameter 2 4 6. Next, we print the output to the console. Notice that the output is true. We use the number predicate function to check if the passed values, which are numbers, pass the test by the even predicate function. We use the even predicate function to see if a number is even.
  • Line 6: We call the function func.

RELATED TAGS

predicate function
clojure
every- pred
communitycreator

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