gpt4 book ai didi

clojure - seq 和 seq 和有什么不一样?

转载 作者:行者123 更新时间:2023-12-04 22:49:56 29 4
gpt4 key购买 nike

-------------------------
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
-----

明显是空的?是基于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))

和更多:
(not (seq? ())) ;;false
(not (seq ())) ;;true
(not nil) ;;true

最佳答案

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

  • 我猜想 (seq x) empty? 的文档字符串中的成语适用于使用 if-let 的常见做法像这样:
    (defn print-odd-numbers [coll]
    (if-let [x (seq (filter odd? coll))]
    (println "Odd numbers:" x)
    (println "No odd numbers found.")))

    关于clojure - seq 和 seq 和有什么不一样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9025124/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com