无涯教程-Clojure - Predicates函数

63 阅读1分钟

Predicates是判断条件并提供true或false值的函数,所有这些函数都返回true或false值。

以下是Clojure中的Predicates示例。

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

;; This program displays Hello Learnfk (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

除正常predicates函数外,Clojure还为谓predicates提供了更多函数。

Sr.No. Methods & 描述
1 every-pred

接受一组Predicates,并返回一个函数" f",如果所有组成Predicates针对所有参数返回逻辑真值,则返回true,否则返回false。

2 every?

如果Predicates对于每个值都为true,则返回true,否则为false。

3 some

返回值集合中x的任何predicates值的第一个逻辑真值。

4 not-any?

如果集合中值的任何predicates在逻辑上为true,则返回false,否则返回true。

参考链接

www.learnfk.com/clojure/clo…