meander

All things about https://github.com/noprompt/meander Need help and no one responded? Feel free to ping @U5K8NTHEZ
2021-01-11T11:50:42.157800Z

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]))

2021-01-11T16:31:49.158200Z

@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]

2021-01-11T16:37:57.158700Z

(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}}

2021-01-11T16:52:37.159400Z

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)))

2021-01-11T16:56:03.159800Z

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

2021-01-11T16:57:24.160100Z

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.

2021-01-11T17:07:47.160500Z

(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}

2021-01-11T17:09:03.160700Z

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

2021-01-11T17:09:41.161Z

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

jimmy 2021-01-11T17:16:01.161600Z

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

πŸ‘ 2
jimmy 2021-01-12T17:06:51.163200Z

Sorry, I was busier than expected.

noprompt 2021-01-11T17:50:58.163Z

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