What is the some predicate function in Clojure?

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)

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.

Free Resources