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 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:
{: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)
I supouse that is a trick with the vars but i dont know how it really works. Any reading you can provide i will be gratefull
---EDIT--- The f/index-forward and f/encoder-icase notations works fine.
---EDIT 2--- i've defined:
(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 –
I think the "No such var" error is happening because the tokenizer is an anonymous function.
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.
However, in general, you cannot compare two functions for equality - as @cfrick says. When you are comparing maps, where some of the values are functions, you are still comparing functions.