nth not supported on this type: Keyword
我是Clojure的新手,作为学习练习,我试图编写一个函数来验证映射中键的存在。
当我尝试运行下面的代码时,出现错误提示
java.lang.UnsupportedOperationException: nth not supported on this type: Keyword
1 2 3 4 5 6 7 8 9 10 | (def record {:name"Foobar"}) (def validations [:name (complement nil?)]) (defn validate [candidate [key val]] (val (get candidate key)) (def actual (every? (partial validate record) validations)) (= true actual) |
据我了解,我只是部分地应用了validate函数,并在地图上声明了每个validate函数-但这似乎不起作用-所以我一定会误会某些东西吗?
该错误来自您在validate:
您的问题是要向
要解决此问题,您需要使验证列表像这样列出列表:
1 2 3 4 5 6 7 8 9 10 | (def record {:name"Foobar"}) (def validations [[:name (complement nil?)]]) (defn validate [candidate [key val]] (val (get candidate key))) (def actual (every? (partial validate record) validations)) (= true actual) ;; => true |
这样,
众所周知,在新的Clojure版本(1.6及更高版本)中,现在有一个名为
1 2 3 4 5 6 7 8 9 10 | (def record {:name"Foobar"}) (def validations [:name (complement nil?)]) (defn validate [candidate [key val]] (val (get candidate key))) (def actual (every? (partial validate record) [validations])) (= true actual) |
顺便说一句,您可以使用