簡體   English   中英

當結果是一個函數時,無法使測試為真(編譯時出現語法錯誤.... 沒有這樣的 var:...)

[英]Can't get test to true when the result is a function (Syntax error compiling at .... No such var:...)

當我想測試結果是另一個函數的函數時會發生這種情況。 我有這樣的事情:

ns flexsearch.core

(defn init [{:keys [tokenizer split indexer filter] :as options}]
  (let [encoder (get-encoder (:encoder options))]
    (assoc (merge {:ids {} :data {}} options)
           :indexer (get-indexer indexer)
           :encoder encoder
           :tokenizer (if (fn? tokenizer) tokenizer #(string/split % (or split #"\W+")))
           :filter (set (mapv encoder filter)))))

在測試 ns 中:

ns flexsearch.core-test
[flexsearch.core :as f]

(def split #"\W+")

(is (= (f/init {:tokenizer false :split split :indexer :forward :filter #{"and" "or"}})
         {:ids {},
          :data {},
          :tokenizer f/init/fn--14976,
          :split #"\W+",
          :indexer f/index-forward,
          :filter #{"or" "and"},
          :encoder f/encoder-icase}))

repl 中的結果是:

{:ids {},
 :data {},
 :tokenizer #function[flexsearch.core/init/fn--14976],
 :split #"\W+",
 :indexer #function[flexsearch.core/index-forward],
 :filter #{"or" "and"},
 :encoder #function[flexsearch.core/encoder-icase]}

我知道我必須把 f/index-forward 而不是 repl [flexsearch.core/index-forward] 的結果,但它不適用於 f/init/fn--14976(沒有這樣的 var:f /init/fn--14976)

我認為這是 vars 的一個技巧,但我不知道它是如何工作的。 您能提供的任何閱讀資料,我將不勝感激

---編輯--- f/index-forward 和 f/encoder-icase 符號工作正常。

---編輯 2--- 我已經定義了:

(defn spliter [split]   (fn [x] (string/split x (or split #"\W+"))))

並將其用於:

(defn init [{:keys [tokenizer split indexer filter] :as options}]
  (let [encoder (get-encoder (:encoder options))]
    (assoc (merge {:ids {} :data {}} options)
           :indexer (get-indexer indexer)
           :encoder encoder
           :tokenizer (if (fn? tokenizer) tokenizer (spliter split))
           :filter (set (mapv encoder filter)))))

我在測試中使用了類似的“:tokenizer #function[flexsearch.core/spliter/fn--34857]”,但它也失敗了 –

認為發生“No such var”錯誤是因為標記器是一個匿名函數。

如果您在flexsearch.core中將默認標記器定義為非匿名函數,然后在測試中使用該名稱,它就會起作用。

但是,一般來說,您不能比較兩個函數的相等性 - 正如@cfrick 所說。 當您比較地圖時,其中一些值是函數,您仍在比較函數。

暫無
暫無

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

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