Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

predicate function
clojure
communitycreator

What is the some 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 (true or false). We have predicate functions like the even number. You can search for these shots to see their functionality as each predicate function has been treated in detail separately.

What is the some function?

The some function takes in a predicate method to evaluate the parameter passed. While evaluating the parameters, the some function returns true on the first parameter that satisfies the predicate method.

Syntax

(some? p1 pn)
The some syntax

Here, p1 is the predicate function and pn is the parameter to be tested.

Parameter

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

Return value

The some function returns true once it finds the parameter that satisfies the predicate method. It returns nil if none of the parameters satisfies the predicate method.

Example

(ns clojure.examples.example
(:gen-class))
(defn func []
(println (some zero? '(2 4 6))))
(func)
An example of the some function

Explanation

  • Line 3: We define a function, func.
  • Line 4: We use a predicate method (zero) alongside the some function to evaluate the passed parameter 2 4 6. Then, we print out the output. Notice that the output is nil because even though the values passed are numbers, all values fail the test by the zero predicate function because the zero predicate function checks to see if a number is zero.
  • Line 6: We call the func function.

RELATED TAGS

predicate function
clojure
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