我是Clojure的新手。我试图运行这个并且找不到符号解析到字典。
(ns noobfile
(:require '[clojure.string :as str]
'[noobfile]))
(def my_str "1|John Smith|123 Here Street|456-4567
2|Sue Jones|43 Rose Court Street|345-7867
3|Fan Yuhong|165 Happy Lane|345-4533")
(def my_dict (str/split my_str #"\n"))
(defn pasre-to-dict [x] (str ":" x))
(apply parse-to-dict my_dict)
错误如下:
CompilerException java.lang.Exception: Found lib name 'clojure.string' containing period with prefix 'quote'. lib names inside prefix lists must not contain periods, compiling:(/tmp/form-init6237588243498764600.clj:16:1)
CompilerException java.lang.RuntimeException: Unable to resolve symbol: parse-to-dict in this context, compiling:(/tmp/form-init6237588243498764600.clj:28:1)
答案 0 :(得分:2)
require
条款中不应有引号。noobfile
。(defn pasre-to-dict [x] (str ":" x))
中有拼写错误。apply
没有意义。您可能希望根据要实现的目标修改parse-to-dict
函数。所以你可以继续这里:
(ns noobfile
(:require [clojure.string :as str]))
(def my_str "1|John Smith|123 Here Street|456-4567
2|Sue Jones|43 Rose Court Street|345-7867
3|Fan Yuhong|165 Happy Lane|345-4533")
(def my_dict (str/split my_str #"\n"))
(defn parse-to-dict [x] (str ":" x))
(parse-to-dict my_dict)
请在下次提问之前自己做更多的研究。
答案 1 :(得分:0)
答案 2 :(得分:0)
我会选择这样的事情:
(require '[clojure.string :as cs])
(reduce #(let [[x & xs] (cs/split (cs/trim %2) #"\|")]
(assoc %1 x xs))
{}
(cs/split-lines my_str))
;; => {"1" ("John Smith" "123 Here Street" "456-4567"),
;; "2" ("Sue Jones" "43 Rose Court Street" "345-7867"),
;; "3" ("Fan Yuhong" "165 Happy Lane" "345-4533")}
但是你应该在开始之前阅读一些clojure基础知识,正如大家所建议的那样。