Fork me on GitHub
#meander
<
2021-01-11
>
mac11:01:42

Why would the below fail with Execution error (IllegalArgumentException) at meander.interpreter.epsilon/match-clause-factory$fn$fn$fn (epsilon.cljc:490). Key must be integer :

(let [q ['?y '?x]
      f (mi/searcher
         q ['?x])]
  (f [:x 1]))

ribelo16:01:49

@mac

(defn project
  "Helper which extracts the values of vars (keys) out a map in the
  order they are given.
  Example:
    (let [f (mi/finder '[?x ?y] (project '[?y ?x ?y]))]
      (f [1 2]))
    ;; => [2 1 2]"
  [vars]
  (fn [bindings] (mapv bindings vars)))

(let [q '[?y ?x]
      f (mi/searcher
         q (project ['?x]))]
  (f [:x 1]))
;; => [1]

ribelo16:01:57

(defn debug
  [vars]
  (fn [bindings]
    {:vars vars
     :bindings bindings}))

(let [q '[?y ?x]
      f (mi/finder
         q (debug '?x))]
  (f [:x 1]))
;; =>
;; {:vars ?x,
;;  :bindings {:meander.interpreter.epsilon/cata #function
;;             [meander.interpreter.epsilon/match-system-factory/fn--23289/f--23292] ,
;;             ?y :x,
;;             ?x 1}}

ribelo16:01:37

I suspect that if the data being thrown into the function returned by the intrepreter is a vector, it unfolds to something similar

(let [date [:x 1])
      l ['?y '?x]
      r '?x]
  (let [idx (.indexOf l r)]
    (.get data idx)))

ribelo16:01:03

I guess this is a bug, because if idx is nil, the rest of the function should not be continued.

ribelo16:01:24

If you want to do something a bit more complicated then you should create a function that takes a query and that returns a function that takes a hasmap with the bindings.

ribelo17:01:47

(let [q '[?y ?x]
      f (mi/finder
         q {:a '?x})]
  (f [:x 1]))
;; => nil

(let [q '[?y ?x]
      f (mi/finder
         q ((fn [x] (fn [bindings] {:a (get bindings x)})) '?x))]
  (f [:x 1]))
;; => {:a 1}

ribelo17:01:03

Although IMHO does not make sense, it is better to build the collection that we need after obtaining the value

ribelo17:01:41

(let [q '[?y ?x]
      f (mi/finder
         q '?x)]
  {:a (f [:x 1])})

Jimmy Miller17:01:01

Will take a look at this later today is someone else doesn't get to it first.

👍 6
noprompt17:01:58

Yeah, the interpreter stuff likely has rough edges. Please also report these on Github too if you don’t mind. 🙂