我正在尝试从此代码中获取一组信息,但出现异常:
Execution error (ClassCastException) at user/get-non-friends-set (form-init1338839880564085393.clj:7).
clojure.core$set cannot be cast to clojure.lang.IPersistentCollection
这是我的代码:
(defn get-non-friends-set [id]
(let [non-friends set]
(doseq [i (get-friends-set id)]
(doseq [j (get-friends-set i)]
(if (and (false? (is-friend? j id))
(false? (is-privacy-on? j)))
(conj non-friends j)
non-friends)))))
逻辑工作正常,如果我将(conj non-friends j)
替换为(println j)
,则得到了期望的(j)
输出,但是我需要该函数返回一个集合。
答案 0 :(得分:2)
您试图返回一个函数,而不是调用该函数并返回结果。您需要(hash-set)
而不是set
-注意括号和名称更改。
但是,通常将简短的空集写为#{}
而不是调用函数通常会更短,更清晰:
(let [non-friends #{} ]
...)
vs
(let [non-friends (hash-set) ]
...)
但是,您还有另一个问题,就是您的2个doseq
循环都将始终返回nil
。您可能想改用filter
或filterv
函数。
请参见
Getting Clojure
,Living Clojure
之类的书