📜  Clojure-谓词

📅  最后修改于: 2020-11-05 04:04:10             🧑  作者: Mango


谓词是评估条件并提供true或false值的函数。在数字一章的示例中,我们已经看到谓词功能。我们已经看到了诸如“偶数”之类的功能。用于测试数字是否为偶数或“负”?用于测试数字是否大于零。所有这些函数都返回true或false值。

以下是Clojure中的谓词示例。

(ns clojure.examples.example
   (:gen-class))

;; This program displays Hello World
(defn Example []
   (def x (even? 0))
   (println x)
   
   (def x (neg? 2))
   (println x)
   
   (def x (odd? 3))
   (println x)
   
   (def x (pos? 3))
   (println x))
(Example)

上面的程序产生以下输出。

true
false
true
true

除正常谓词功能外,Clojure还为谓词提供了更多功能。以下方法可用于谓词。

Sr.No. Methods & Description
1 every-pred

Takes a set of predicates and returns a function ‘f’ that returns true if all of its composing predicates return a logical true value against all of its arguments, else it returns false.

2 every?

Returns true if the predicate is true for every value, else false.

3 some

Returns the first logical true value for any predicate value of x in the collection of values.

4 not-any?

Returns false if any of the predicates of the values in a collection are logically true, else true.