簡體   English   中英

無法在類路徑上找到apply /clojure/core/vector__init.class或apply / clojure / core / vector.clj

[英]Could not locate apply /clojure/core/vector__init.class or apply/clojure/core/vector.clj on classpath

我嘗試運行程序時遇到以下錯誤: Exception in thread "main" java.io.FileNotFoundException: Could not locate apply /clojure/core/vector__init.class or apply/clojure/core/vector.clj on classpath: , compiling:(erbium/compile.clj:1:1) 它似乎指向下面的文件,並建議我需要在我的依賴項中放入clojure.core/vector 默認情況下不包括嗎?

(ns erbium.compile
    (require `[clojure.string :as string])
)

(defn produce-out "Convert 'command %1 %2 %3' with stack [5 6 7] to 'command 5 6 7'" [word stack definitions]
    (let [
            code (definitions word) ; dictionary/hash lookup. eg. "println" -> "echo $1"
            replacement (fn [match] (-> match second Long/parseLong dec stack str))
         ]
        ; evaluate arguments. eg. "echo %1"
        ;                         stack=["blah"]
        ;                         -> "echo blah"
        (string/replace code #"%(\d)" replacement)
    )
)

(defn parse-word "Verifies that word is in defintitions and then returns (produce-out word stack)" [word stack definitions]
    (if (some #{word} (keys definitions))
        (produce-out word stack)
    )
)

(defn compile "Main compile function" [code]
    (let [
            split-code (string/split code #"\s")
            definitions {
                "println" "echo %1"
                "+" "%1 + %2"
                "-" "%1 - %2"
            }
            stack []
        ]

        (for [word [split-code]]
            (if (integer? (read-string word))
                (do
                    (println "Found integer" word)
                    (def stack (conj stack (read-string word)))
                    (println "Adding to argument stack:" stack)
                )

                ; else
                (do
                    (parse-word word stack definitions)
                    (def stack [])
                )
            )
        )
    )
)

如果有所不同,則此文件由核心文件通過(load "compile")

我看到的第一個錯誤是:

(require `[clojure.string :as string])

應該是這樣的:

(:require [clojure.string :as string])

在常規clojure源文件中。 這為我解決了。

也就是說,這里有一些一般性建議:

  1. 有很多格式化“錯誤”。 當然,您可以按照自己的意願來格式化代碼,但是,如果您遵循基本的格式化原則,那么其他人就更容易遵循。 這是它們的一個很好的集合: https : //github.com/bbatsov/clojure-style-guide大多數編輯器都實現了一些格式化工具。
  2. split-code (str/split code #"\\s")不能按您的要求運行[clojure.string :as string]因此將其更改為: split-code (string/split code #"\\s")
  3. 我不確定(load ...)以及在哪種情況下通常使用它。 但是,為了開始學習Clojure,我建議您使用Lighttable,因為它內置了即時反饋,在學習新東西時非常有價值。

為了擴大答案,“ require”出現在名稱空間聲明“ ns”中。 ns實際上是一個宏,它擴展為一系列語句以創建名稱空間並進行一些設置。

該宏將(:require ...)類的語句視為對名稱為require的函數的調用,並自動引用以下任何參數。 由於您自己指定了報價:

(ns erbium.compile
  (require '[clojure.string :as string]))

然后結果最終被雙引號引起,對require的調用最終成為:

... (require (quote (quote [clojure.string :as string])))

因此,最終會嘗試加載一個名為“ quote”的名稱空間,然后加載一個語法錯誤的向量。 :)

ns宏是設置名稱空間的一種標准且便捷的方法,但是花了我很長時間才能正確學習它。 我發現最好的方法是復制其他人的設置代碼,直到我了解如何正確執行它為止。

順便說一句,雖然標准是使用:require所以使用require代替:require並不重要,因此它看起來並不像對該函數的直接調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM