ClojureDocs

导航

命名空间

not-empty

clojure.core

1.0 版及以后版本可用 (源代码)
  • (not-empty coll)
If coll is empty, returns nil, else coll
3 Examples
user=> (not-empty [1])
[1]
user=> (not-empty [1 3 5])
[1 3 5]
user=> (not-empty [])
nil
user=> (not-empty '())
nil
user=> (not-empty {})
nil
user=> (not-empty nil)
nil
;; Same behaviour for strings

user> (not-empty "hello")
"hello"

user> (not-empty "")
nil
;; if-let can now work with colls also, not being limited to scalar values alone
user> (if-let [valid (not-empty (filter even? [2 4 6]))] valid)
(2 4 6)

;; if-let helps avoid another nested if check
user> (if-let [valid (not-empty (filter even? [1 3 5]))] valid)
nil

;; without not-empty we would have to work with another if conditional
user> (if-let [valid (filter even? [1 3 5])] valid)
()
See Also

Returns an empty collection of the same category as coll, or nil

Returns true if coll has no items - same as (not (seq coll)). Please use the idiom (seq x) rather ...

Added by BernhardBln

Returns a seq on the collection. If the collection is empty, returns nil. (seq nil) returns nil...

Added by jswalens
0 Notes
No notes for not-empty