【问题标题】:Getting Vars to bind properly across multiple files让 Vars 跨多个文件正确绑定
【发布时间】:2010-03-20 03:48:25
【问题描述】:

我刚刚学习 Clojure,无法将我的代码移动到不同的文件中。

我不断地从 appnrunner.clj 中发现这个错误 -

Exception in thread "main" java.lang.Exception: Unable to resolve symbol: -run-application in this context

似乎发现命名空间很好,但没有看到 Vars 被绑定...知道如何解决这个问题吗?

这是我的代码:

应用程序运行器 -

(ns src/apprunner
  (:use src/functions))

(def input-files [(resource-path "a.txt") (resource-path "b.txt") (resource-path "c.txt")])
(def output-file (resource-path "output.txt"))

(defn run-application []
  (sort-files input-files output-file))

(run-application)

应用功能-

(ns src/functions
  (:use clojure.contrib.duck-streams))

(defn flatten [x]
  (let [s? #(instance? clojure.lang.Sequential %)]
    (filter
      (complement s?)
      (tree-seq s? seq x))))

(defn resource-path [file]
  (str "C:/Users/Alex and Paula/Documents/SoftwareProjects/MyClojureApp/resources/" file))

(defn split2 [str delim]
  (seq (.split str delim)))

(defstruct person :first-name :last-name)

(defn read-file-content [file]
  (apply str
    (interpose "\n" (read-lines file))))

(defn person-from-line [line]
  (let [sections (split2 line " ")]
    (struct person (first sections) (second sections))))

(defn formatted-for-display [person]
  (str (:first-name person) (.toUpperCase " ") (:last-name person)))

(defn sort-by-keys [struct-map keys]
  (sort-by #(vec (map % [keys])) struct-map))

(defn formatted-output [persons output-number]
  (let [heading (str "Output #" output-number "\n")
        sorted-persons-for-output (apply str (interpose "\n" (map formatted-for-display (sort-by-keys persons (:first-name :last-name)))))]
    (str heading sorted-persons-for-output)))

(defn read-persons-from [file]
  (let [lines (read-lines file)]
    (map person-from-line lines)))

(defn write-persons-to [file persons]
  (dotimes [i 3]
    (append-spit file (formatted-output persons (+ 1 i)))))

(defn sort-files [input-files output-file]
  (let [persons (flatten (map read-persons-from input-files))]
    (write-persons-to output-file persons)))

【问题讨论】:

    标签: clojure


    【解决方案1】:

    你没有正确命名你的命名空间!

    你应该这样做:


    ;;; in file src/apprunner/core.clj
    ;;; (namespace names should contain at least one dot for things
    ;;; not to go into the default package... or some such thing)
    (ns apprunner.core
      (:use apprunner.functions)
    
    ;;; the rest of your code for this file follows unchanged
    

    ;;; in file src/apprunner/functions.clj
    (ns apprunner.functions
      (:use clojure.contrib.duck-streams))
    
    ;;; the rest of your code for this file follows unchanged
    

    在 REPL((use 'apprunner.core) 等)上运行上述内容对我来说效果很好。

    总结一下这里的问题:命名空间名称应该包含点,其中定义它们的文件的路径包含斜杠/反斜杠(不是我的意思是 relative 路径 - 相对于实际位于的某个目录类路径)。此外,src/ 目录是您放在类路径中的目录,因此您不会在命名空间名称中包含 src. 部分。参见例如我对your earlier question 的回答中的src/foo/bar/baz.cljfoo.bar.baz 示例。

    哦,顺便说一句,找出类路径困难的,期间。因此,请务必不要让自己因此类问题而气馁! :-) 如果您有更多与命名空间或类路径相关的问题,或者以上内容不能为您解决问题,您可能希望包含有关如何运行代码的信息。

    【讨论】:

      猜你喜欢
      • 2013-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多