当我想测试结果是另一个函数的函数时发生。我有这样的事情:
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 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 (No such 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]”,我在测试中使用它,它也失败了——</p>