[英]Can't get test to true when the result is a function (Syntax error compiling at .... No such var:...)
Happens when I want to test a function where the result is another function.当我想测试结果是另一个函数的函数时会发生这种情况。 I have something like this:我有这样的事情:
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)))))
And in the test ns:在测试 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}))
the result in the repl is: 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]}
I know that I have to put f/index-forward instead of the result of the repl [flexsearch.core/index-forward], but it doesn't work with f/init/fn--14976 (No such var: f/init/fn--14976)我知道我必须把 f/index-forward 而不是 repl [flexsearch.core/index-forward] 的结果,但它不适用于 f/init/fn--14976(没有这样的 var:f /init/fn--14976)
I supouse that is a trick with the vars but i dont know how it really works.我认为这是 vars 的一个技巧,但我不知道它是如何工作的。 Any reading you can provide i will be gratefull您能提供的任何阅读资料,我将不胜感激
---EDIT--- The f/index-forward and f/encoder-icase notations works fine. ---编辑--- f/index-forward 和 f/encoder-icase 符号工作正常。
---EDIT 2--- i've defined: ---编辑 2--- 我已经定义了:
(defn spliter [split] (fn [x] (string/split x (or split #"\W+"))))
and used it on:并将其用于:
(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)))))
the I get a similar ":tokenizer #function[flexsearch.core/spliter/fn--34857]," that I used in the test and it also failed –我在测试中使用了类似的“:tokenizer #function[flexsearch.core/spliter/fn--34857]”,但它也失败了 –
I think the "No such var" error is happening because the tokenizer is an anonymous function.我认为发生“No such var”错误是因为标记器是一个匿名函数。
If you had the default tokenizer defined as a non-anonymous function in flexsearch.core
and then used that name in the test, it would work.如果您在flexsearch.core
中将默认标记器定义为非匿名函数,然后在测试中使用该名称,它就会起作用。
However, in general, you cannot compare two functions for equality - as @cfrick says.但是,一般来说,您不能比较两个函数的相等性 - 正如@cfrick 所说。 When you are comparing maps, where some of the values are functions, you are still comparing functions.当您比较地图时,其中一些值是函数,您仍在比较函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.