seq和seq有什么区别?

lka*_*htz 4 clojure

-------------------------
clojure.core/seq
([coll])
  Returns a seq on the collection. If the collection is
    empty, returns nil.  (seq nil) returns nil. seq also works on
    Strings, native Java arrays (of reference types) and any objects
    that implement Iterable.
-------------------------
clojure.core/seq?
([x])
  Return true if x implements ISeq
-----
Run Code Online (Sandbox Code Playgroud)

显然是空的?基于seq.空的区别是什么?没有?我很困惑.

clojure.core/empty?
([coll])
  Returns true if coll has no items - same as (not (seq coll)).
  Please use the idiom (seq x) rather than (not (empty? x))
Run Code Online (Sandbox Code Playgroud)

和更多:

(not (seq? ())) ;;false
(not (seq ())) ;;true
(not nil) ;;true
Run Code Online (Sandbox Code Playgroud)

Mis*_*hor 10

  • seq将集合转换为序列,如果集合为空,则返回nil; 如果参数为nil,则返回nil.
  • seq? 如果参数是序列(实现ISeq接口),则返回true.
  • empty? 如果参数为nil或空集合,则返回true.
  • nil? 如果参数为零,则返回true.

我想(seq x)在docstring中关于成语的一点empty?适用于使用if-let这样的常规做法:

(defn print-odd-numbers [coll]
  (if-let [x (seq (filter odd? coll))]
    (println "Odd numbers:" x)
    (println "No odd numbers found.")))
Run Code Online (Sandbox Code Playgroud)