Clojure中Seq函数的使用

The use of Seq function in Clojure

manual of clojure 关于 seq 我们读到:;; (seq x) is the recommended idiom for testing if a collection is not empty (every? seq ["1" [1] '(1) {:1 1} #{1}]) ;;=> true。但是空集合 returns 本身也是 nil,那么使用 seq 来测试集合是否为空有什么意义呢?

来自该页面顶部的文档:

seq also works on Strings, native Java arrays (of reference types) and any objects that implement Iterable

因此使用 seq 来测试空性适用于任何这些集合类型。因此,如示例所示,您将获得一种一致的惯用方法来检查这些类型中的任何一种是否为空。

seq returns nil 在空集合和 nil 上的事实也使检查更简单,而不是需要检查空或 nil。

空集合不是假的,所以在测试中它是否为空并不重要:

(if '() "a" "b")
=> "a"

所以如果你想在它是空的时候做点别的:

(if (seq '()) "a" "b")
=> "b"